1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009-2012 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef THREAD_H_
#define THREAD_H_
/* Thread start routine signature for all OSes */
typedef void (*thread_func_t)(void *user_data);
#ifdef __Windows__
#include <windows.h>
/* Windows threads. Threads are identified by a HANDLE, which becomes
* signalled when the thread exits.
*
* thread_create() returns 0 on success or -1 if an error occurs.
*/
typedef HANDLE thread_t;
static inline int thread_create(thread_t *t, thread_func_t func, void *arg)
{
*t = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg,
0, NULL);
return (*t) ? 0 : -1;
}
static inline void thread_join(thread_t t)
{
WaitForSingleObject(t, INFINITE);
}
/* Windows mutexes. We use critical sections, because we don't need to
* share between processes.
*
* None of these functions are expected to fail, although
* InitializeCriticalSection may raise an exception on some versions of
* Windows under low memory conditions.
*/
typedef CRITICAL_SECTION thread_lock_t;
static inline void thread_lock_init(thread_lock_t *lock)
{
InitializeCriticalSection(lock);
}
static inline void thread_lock_destroy(thread_lock_t *lock)
{
DeleteCriticalSection(lock);
}
static inline void thread_lock_acquire(thread_lock_t *lock)
{
EnterCriticalSection(lock);
}
static inline void thread_lock_release(thread_lock_t *lock)
{
LeaveCriticalSection(lock);
}
/* Windows condition variables. These are simulated using kernel event
* objects. Note that this implementation is correct _only_ for the
* case of a single waiter.
*/
typedef HANDLE thread_cond_t;
static inline void thread_cond_init(thread_cond_t *c)
{
*c = CreateEvent(0, TRUE, FALSE, NULL);
}
static inline void thread_cond_destroy(thread_cond_t *c) {
CloseHandle(*c);
}
static inline void thread_cond_wait(thread_cond_t *c, thread_lock_t *m)
{
thread_lock_release(m);
WaitForSingleObject(*c, INFINITE);
thread_lock_acquire(m);
ResetEvent(*c);
}
static inline void thread_cond_notify(thread_cond_t *c)
{
SetEvent(*c);
}
#else /* __Windows__ */
#include <pthread.h>
/* POSIX thread creation. */
typedef pthread_t thread_t;
static inline int thread_create(thread_t *t, thread_func_t func, void *arg)
{
return pthread_create(t, NULL, (void *(*)(void *))func, arg);
}
static inline void thread_join(thread_t t)
{
pthread_join(t, NULL);
}
/* POSIX mutexes. */
typedef pthread_mutex_t thread_lock_t;
static inline void thread_lock_init(thread_lock_t *lock)
{
pthread_mutex_init(lock, NULL);
}
static inline void thread_lock_destroy(thread_lock_t *lock)
{
pthread_mutex_destroy(lock);
}
static inline void thread_lock_acquire(thread_lock_t *lock)
{
pthread_mutex_lock(lock);
}
static inline void thread_lock_release(thread_lock_t *lock)
{
pthread_mutex_unlock(lock);
}
/* POSIX condition variables. */
typedef pthread_cond_t thread_cond_t;
static inline void thread_cond_init(thread_cond_t *c)
{
pthread_cond_init(c, NULL);
}
static inline void thread_cond_destroy(thread_cond_t *c)
{
pthread_cond_destroy(c);
}
static inline void thread_cond_wait(thread_cond_t *c, thread_lock_t *m)
{
pthread_cond_wait(c, m);
}
static inline void thread_cond_notify(thread_cond_t *c)
{
pthread_cond_signal(c);
}
#endif
#endif
|