| 1 | /***************************************************************************** |
| 2 | Copyright (c) 1995, 2014, Oracle and/or its affiliates. All Rights Reserved. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify it under |
| 5 | the terms of the GNU General Public License as published by the Free Software |
| 6 | Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License along with |
| 13 | this program; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 15 | |
| 16 | *****************************************************************************/ |
| 17 | |
| 18 | /**************************************************//** |
| 19 | @file include/os0event.h |
| 20 | The interface to the operating system condition variables |
| 21 | |
| 22 | Created 2012-09-23 Sunny Bains (split from os0sync.h) |
| 23 | *******************************************************/ |
| 24 | |
| 25 | #ifndef os0event_h |
| 26 | #define os0event_h |
| 27 | |
| 28 | #include "univ.i" |
| 29 | |
| 30 | // Forward declaration. |
| 31 | struct os_event; |
| 32 | typedef struct os_event* os_event_t; |
| 33 | |
| 34 | /** Denotes an infinite delay for os_event_wait_time() */ |
| 35 | #define OS_SYNC_INFINITE_TIME ULINT_UNDEFINED |
| 36 | |
| 37 | /** Return value of os_event_wait_time() when the time is exceeded */ |
| 38 | #define OS_SYNC_TIME_EXCEEDED 1 |
| 39 | |
| 40 | /** |
| 41 | Creates an event semaphore, i.e., a semaphore which may just have two states: |
| 42 | signaled and nonsignaled. The created event is manual reset: it must be reset |
| 43 | explicitly by calling os_event_reset(). |
| 44 | @return the event handle */ |
| 45 | os_event_t os_event_create(const char*); |
| 46 | |
| 47 | /** |
| 48 | Sets an event semaphore to the signaled state: lets waiting threads |
| 49 | proceed. */ |
| 50 | void |
| 51 | os_event_set( |
| 52 | /*=========*/ |
| 53 | os_event_t event); /*!< in/out: event to set */ |
| 54 | |
| 55 | /** |
| 56 | Check if the event is set. |
| 57 | @return true if set */ |
| 58 | bool |
| 59 | os_event_is_set( |
| 60 | /*============*/ |
| 61 | const os_event_t event); /*!< in: event to set */ |
| 62 | |
| 63 | /** |
| 64 | Resets an event semaphore to the nonsignaled state. Waiting threads will |
| 65 | stop to wait for the event. |
| 66 | The return value should be passed to os_even_wait_low() if it is desired |
| 67 | that this thread should not wait in case of an intervening call to |
| 68 | os_event_set() between this os_event_reset() and the |
| 69 | os_event_wait_low() call. See comments for os_event_wait_low(). */ |
| 70 | int64_t |
| 71 | os_event_reset( |
| 72 | /*===========*/ |
| 73 | os_event_t event); /*!< in/out: event to reset */ |
| 74 | |
| 75 | /** |
| 76 | Frees an event object. */ |
| 77 | void |
| 78 | os_event_destroy( |
| 79 | /*=============*/ |
| 80 | os_event_t& event); /*!< in/own: event to free */ |
| 81 | |
| 82 | /** |
| 83 | Waits for an event object until it is in the signaled state. |
| 84 | |
| 85 | Typically, if the event has been signalled after the os_event_reset() |
| 86 | we'll return immediately because event->is_set == TRUE. |
| 87 | There are, however, situations (e.g.: sync_array code) where we may |
| 88 | lose this information. For example: |
| 89 | |
| 90 | thread A calls os_event_reset() |
| 91 | thread B calls os_event_set() [event->is_set == TRUE] |
| 92 | thread C calls os_event_reset() [event->is_set == FALSE] |
| 93 | thread A calls os_event_wait() [infinite wait!] |
| 94 | thread C calls os_event_wait() [infinite wait!] |
| 95 | |
| 96 | Where such a scenario is possible, to avoid infinite wait, the |
| 97 | value returned by os_event_reset() should be passed in as |
| 98 | reset_sig_count. */ |
| 99 | void |
| 100 | os_event_wait_low( |
| 101 | /*==============*/ |
| 102 | os_event_t event, /*!< in/out: event to wait */ |
| 103 | int64_t reset_sig_count);/*!< in: zero or the value |
| 104 | returned by previous call of |
| 105 | os_event_reset(). */ |
| 106 | |
| 107 | /** Blocking infinite wait on an event, until signealled. |
| 108 | @param e - event to wait on. */ |
| 109 | #define os_event_wait(e) os_event_wait_low((e), 0) |
| 110 | |
| 111 | /** |
| 112 | Waits for an event object until it is in the signaled state or |
| 113 | a timeout is exceeded. In Unix the timeout is always infinite. |
| 114 | @return 0 if success, OS_SYNC_TIME_EXCEEDED if timeout was exceeded */ |
| 115 | ulint |
| 116 | os_event_wait_time_low( |
| 117 | /*===================*/ |
| 118 | os_event_t event, /*!< in/out: event to wait */ |
| 119 | ulint time_in_usec, /*!< in: timeout in |
| 120 | microseconds, or |
| 121 | OS_SYNC_INFINITE_TIME */ |
| 122 | int64_t reset_sig_count); /*!< in: zero or the value |
| 123 | returned by previous call of |
| 124 | os_event_reset(). */ |
| 125 | |
| 126 | /** Blocking timed wait on an event. |
| 127 | @param e - event to wait on. |
| 128 | @param t - timeout in microseconds */ |
| 129 | #define os_event_wait_time(e, t) os_event_wait_time_low((e), (t), 0) |
| 130 | |
| 131 | #endif /* !os0event_h */ |
| 132 | |