1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef __FUTURE_H__
20 #define __FUTURE_H__
21 
22 #include "osi/semaphore.h"
23 
24 struct future {
25     bool ready_can_be_called;
26     osi_sem_t semaphore; // NULL semaphore means immediate future
27     void *result;
28 };
29 typedef struct future future_t;
30 
31 #define FUTURE_SUCCESS ((void *)1)
32 #define FUTURE_FAIL ((void *)0)
33 
34 // Constructs a new future_t object. Returns NULL on failure.
35 future_t *future_new(void);
36 
37 // Constructs a new future_t object with an immediate |value|. No waiting will
38 // occur in the call to |future_await| because the value is already present.
39 // Returns NULL on failure.
40 future_t *future_new_immediate(void *value);
41 
42 // Signals that the |future| is ready, passing |value| back to the context
43 // waiting for the result. Must only be called once for every future.
44 // |future| may not be NULL.
45 void future_ready(future_t *future, void *value);
46 
47 // Waits for the |future| to be ready. Returns the value set in |future_ready|.
48 // Frees the future before return. |future| may not be NULL.
49 void *future_await(future_t *async_result);
50 
51 //Free the future if this "future" is not used
52 void future_free(future_t *future);
53 #endif /* __FUTURE_H__ */
54