| 1 | /* Yielding the processor to other threads and processes. |
| 2 | Copyright (C) 2005-2020 Free Software Foundation, Inc. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 3, or (at your option) |
| 7 | any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
| 16 | |
| 17 | /* This file contains a primitive for yielding the processor to other threads. |
| 18 | extern void gl_thread_yield (void); |
| 19 | */ |
| 20 | |
| 21 | #ifndef _GLTHREAD_YIELD_H |
| 22 | #define _GLTHREAD_YIELD_H |
| 23 | |
| 24 | #include <errno.h> |
| 25 | |
| 26 | /* ========================================================================= */ |
| 27 | |
| 28 | #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS |
| 29 | |
| 30 | /* Use the ISO C threads library. */ |
| 31 | |
| 32 | # include <threads.h> |
| 33 | |
| 34 | # ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | # endif |
| 37 | |
| 38 | # define gl_thread_yield() \ |
| 39 | thrd_yield () |
| 40 | |
| 41 | # ifdef __cplusplus |
| 42 | } |
| 43 | # endif |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | /* ========================================================================= */ |
| 48 | |
| 49 | #if USE_POSIX_THREADS |
| 50 | |
| 51 | /* Use the POSIX threads library. */ |
| 52 | |
| 53 | # include <sched.h> |
| 54 | |
| 55 | # ifdef __cplusplus |
| 56 | extern "C" { |
| 57 | # endif |
| 58 | |
| 59 | # define gl_thread_yield() \ |
| 60 | sched_yield () |
| 61 | |
| 62 | # ifdef __cplusplus |
| 63 | } |
| 64 | # endif |
| 65 | |
| 66 | #endif |
| 67 | |
| 68 | /* ========================================================================= */ |
| 69 | |
| 70 | #if USE_WINDOWS_THREADS |
| 71 | |
| 72 | # define WIN32_LEAN_AND_MEAN /* avoid including junk */ |
| 73 | # include <windows.h> |
| 74 | |
| 75 | # ifdef __cplusplus |
| 76 | extern "C" { |
| 77 | # endif |
| 78 | |
| 79 | # define gl_thread_yield() \ |
| 80 | Sleep (0) |
| 81 | |
| 82 | # ifdef __cplusplus |
| 83 | } |
| 84 | # endif |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | /* ========================================================================= */ |
| 89 | |
| 90 | #if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS) |
| 91 | |
| 92 | /* Provide dummy implementation if threads are not supported. */ |
| 93 | |
| 94 | # define gl_thread_yield() 0 |
| 95 | |
| 96 | #endif |
| 97 | |
| 98 | /* ========================================================================= */ |
| 99 | |
| 100 | #endif /* _GLTHREAD_YIELD_H */ |