1 /*****************************************************************************
2 Copyright (c) 1995, 2020, 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, version 2.0, as published by the
6 Free Software Foundation.
7 
8 This program is also distributed with certain software (including but not
9 limited to OpenSSL) that is licensed under separate terms, as designated in a
10 particular file or component or in included license documentation. The authors
11 of MySQL hereby grant you an additional permission to link the program and
12 your derivative works with the separately licensed software that they have
13 included with MySQL.
14 
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
18 for more details.
19 
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 
24 *****************************************************************************/
25 
26 /** @file include/os0event.h
27  The interface to the operating system condition variables
28 
29  Created 2012-09-23 Sunny Bains (split from os0sync.h)
30  *******************************************************/
31 
32 #ifndef os0event_h
33 #define os0event_h
34 
35 #include <sys/types.h>
36 
37 #include "univ.i"
38 
39 // Forward declaration.
40 struct os_event;
41 typedef struct os_event *os_event_t;
42 
43 /** Denotes an infinite delay for os_event_wait_time() */
44 #define OS_SYNC_INFINITE_TIME ULINT_UNDEFINED
45 
46 /** Return value of os_event_wait_time() when the time is exceeded */
47 #define OS_SYNC_TIME_EXCEEDED 1
48 
49 #ifndef UNIV_HOTBACKUP
50 /**
51 Creates an event semaphore, i.e., a semaphore which may just have two states:
52 signaled and nonsignaled. The created event is manual reset: it must be reset
53 explicitly by calling os_event_reset().
54 @return	the event handle */
55 os_event_t os_event_create();
56 
57 /**
58 Sets an event semaphore to the signaled state: lets waiting threads
59 proceed. */
60 void os_event_set(os_event_t event); /*!< in/out: event to set */
61 
62 bool os_event_try_set(os_event_t event);
63 
64 /**
65 Check if the event is set.
66 @return true if set */
67 bool os_event_is_set(const os_event_t event); /*!< in: event to set */
68 
69 /**
70 Resets an event semaphore to the nonsignaled state. Waiting threads will
71 stop to wait for the event.
72 The return value should be passed to os_even_wait_low() if it is desired
73 that this thread should not wait in case of an intervening call to
74 os_event_set() between this os_event_reset() and the
75 os_event_wait_low() call. See comments for os_event_wait_low(). */
76 int64_t os_event_reset(os_event_t event); /*!< in/out: event to reset */
77 
78 /**
79 Frees an event object. */
80 void os_event_destroy(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 os_event_wait_low(os_event_t event,         /*!< in/out: event to wait */
100                        int64_t reset_sig_count); /*!< in: zero or the value
101                                                 returned by previous call of
102                                                 os_event_reset(). */
103 
104 /** Blocking infinite wait on an event, until signealled.
105 @param e - event to wait on. */
106 #define os_event_wait(e) os_event_wait_low((e), 0)
107 
108 /**
109 Waits for an event object until it is in the signaled state or
110 a timeout is exceeded. In Unix the timeout is always infinite.
111 @return 0 if success, OS_SYNC_TIME_EXCEEDED if timeout was exceeded */
112 ulint os_event_wait_time_low(
113     os_event_t event,         /*!< in/out: event to wait */
114     ulint time_in_usec,       /*!< in: timeout in
115                               microseconds, or
116                               OS_SYNC_INFINITE_TIME */
117     int64_t reset_sig_count); /*!< in: zero or the value
118                               returned by previous call of
119                               os_event_reset(). */
120 
121 /** Blocking timed wait on an event.
122 @param e - event to wait on.
123 @param t - timeout in microseconds */
124 #define os_event_wait_time(e, t) os_event_wait_time_low((e), (t), 0)
125 
126 #include "os0event.ic"
127 
128 /** Initializes support for os_event objects. Must be called once,
129  and before any os_event object is created. */
130 void os_event_global_init(void);
131 
132 /** Deinitializes support for os_event objects. Must be called once,
133  and after all os_event objects are destroyed. After it is called, no
134 new os_event is allowed to be created. */
135 void os_event_global_destroy(void);
136 
137 #endif /* !UNIV_HOTBACKUP */
138 #endif /* !os0event_h */
139