| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | Copyright (c) 2017, MariaDB Corporation. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free Software |
| 8 | Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along with |
| 15 | this program; if not, write to the Free Software Foundation, Inc., |
| 16 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 17 | |
| 18 | *****************************************************************************/ |
| 19 | |
| 20 | /**************************************************//** |
| 21 | @file include/os0thread.h |
| 22 | The interface to the operating system |
| 23 | process and thread control primitives |
| 24 | |
| 25 | Created 9/8/1995 Heikki Tuuri |
| 26 | *******************************************************/ |
| 27 | |
| 28 | #ifndef os0thread_h |
| 29 | #define os0thread_h |
| 30 | |
| 31 | #include "univ.i" |
| 32 | |
| 33 | /* Possible fixed priorities for threads */ |
| 34 | #define OS_THREAD_PRIORITY_NONE 100 |
| 35 | #define OS_THREAD_PRIORITY_BACKGROUND 1 |
| 36 | #define OS_THREAD_PRIORITY_NORMAL 2 |
| 37 | #define OS_THREAD_PRIORITY_ABOVE_NORMAL 3 |
| 38 | |
| 39 | #ifdef _WIN32 |
| 40 | typedef DWORD os_thread_t; |
| 41 | typedef DWORD os_thread_id_t; /*!< In Windows the thread id |
| 42 | is an unsigned long int */ |
| 43 | extern "C" { |
| 44 | typedef LPTHREAD_START_ROUTINE os_thread_func_t; |
| 45 | } |
| 46 | |
| 47 | /** Macro for specifying a Windows thread start function. */ |
| 48 | #define DECLARE_THREAD(func) WINAPI func |
| 49 | |
| 50 | #define os_thread_create(f,a,i) \ |
| 51 | os_thread_create_func(f, a, i) |
| 52 | |
| 53 | #else |
| 54 | |
| 55 | typedef pthread_t os_thread_t; |
| 56 | typedef pthread_t os_thread_id_t; /*!< In Unix we use the thread |
| 57 | handle itself as the id of |
| 58 | the thread */ |
| 59 | extern "C" { typedef void* (*os_thread_func_t)(void*); } |
| 60 | |
| 61 | /** Macro for specifying a POSIX thread start function. */ |
| 62 | #define DECLARE_THREAD(func) func |
| 63 | #define os_thread_create(f,a,i) os_thread_create_func(f, a, i) |
| 64 | |
| 65 | #endif /* _WIN32 */ |
| 66 | |
| 67 | /* Define a function pointer type to use in a typecast */ |
| 68 | typedef void* (*os_posix_f_t) (void*); |
| 69 | |
| 70 | #ifdef HAVE_PSI_INTERFACE |
| 71 | /* Define for performance schema registration key */ |
| 72 | typedef unsigned int mysql_pfs_key_t; |
| 73 | #endif /* HAVE_PSI_INTERFACE */ |
| 74 | |
| 75 | /** Number of threads active. */ |
| 76 | extern ulint os_thread_count; |
| 77 | |
| 78 | /***************************************************************//** |
| 79 | Compares two thread ids for equality. |
| 80 | @return TRUE if equal */ |
| 81 | ibool |
| 82 | os_thread_eq( |
| 83 | /*=========*/ |
| 84 | os_thread_id_t a, /*!< in: OS thread or thread id */ |
| 85 | os_thread_id_t b); /*!< in: OS thread or thread id */ |
| 86 | /****************************************************************//** |
| 87 | Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is |
| 88 | unique for the thread though! |
| 89 | @return thread identifier as a number */ |
| 90 | ulint |
| 91 | os_thread_pf( |
| 92 | /*=========*/ |
| 93 | os_thread_id_t a); /*!< in: OS thread identifier */ |
| 94 | /****************************************************************//** |
| 95 | Creates a new thread of execution. The execution starts from |
| 96 | the function given. |
| 97 | NOTE: We count the number of threads in os_thread_exit(). A created |
| 98 | thread should always use that to exit so thatthe thread count will be |
| 99 | decremented. |
| 100 | We do not return an error code because if there is one, we crash here. */ |
| 101 | os_thread_t |
| 102 | os_thread_create_func( |
| 103 | /*==================*/ |
| 104 | os_thread_func_t func, /*!< in: pointer to function |
| 105 | from which to start */ |
| 106 | void* arg, /*!< in: argument to start |
| 107 | function */ |
| 108 | os_thread_id_t* thread_id); /*!< out: id of the created |
| 109 | thread, or NULL */ |
| 110 | |
| 111 | /** Waits until the specified thread completes and joins it. |
| 112 | Its return value is ignored. |
| 113 | @param[in,out] thread thread to join */ |
| 114 | void |
| 115 | os_thread_join( |
| 116 | os_thread_id_t thread); |
| 117 | |
| 118 | /** Exits the current thread. |
| 119 | @param[in] detach if true, the thread will be detached right before |
| 120 | exiting. If false, another thread is responsible for joining this thread */ |
| 121 | ATTRIBUTE_NORETURN ATTRIBUTE_COLD |
| 122 | void os_thread_exit(bool detach = true); |
| 123 | |
| 124 | /*****************************************************************//** |
| 125 | Returns the thread identifier of current thread. |
| 126 | @return current thread identifier */ |
| 127 | os_thread_id_t |
| 128 | os_thread_get_curr_id(void); |
| 129 | /*========================*/ |
| 130 | /*****************************************************************//** |
| 131 | Advises the os to give up remainder of the thread's time slice. */ |
| 132 | void |
| 133 | os_thread_yield(void); |
| 134 | /*=================*/ |
| 135 | /*****************************************************************//** |
| 136 | The thread sleeps at least the time given in microseconds. */ |
| 137 | void |
| 138 | os_thread_sleep( |
| 139 | /*============*/ |
| 140 | ulint tm); /*!< in: time in microseconds */ |
| 141 | |
| 142 | #endif |
| 143 | |