Better threading functions.
Change prefix to "thread_" and add functions for thread creation and condition variables.
This commit is contained in:
parent
5714335614
commit
61fdfd22ff
|
@ -24,7 +24,7 @@
|
|||
#include "dynload.h"
|
||||
#include "tilib.h"
|
||||
#include "tilib_defs.h"
|
||||
#include "threads.h"
|
||||
#include "thread.h"
|
||||
#include "ctrlc.h"
|
||||
|
||||
#if defined(__Windows__) || defined(__CYGWIN__)
|
||||
|
@ -40,7 +40,7 @@ struct tilib_device {
|
|||
|
||||
dynload_handle_t hnd;
|
||||
|
||||
threads_lock_t mb_lock;
|
||||
thread_lock_t mb_lock;
|
||||
uint32_t mailbox;
|
||||
|
||||
uint16_t bp_handles[DEVICE_MAX_BREAKPOINTS];
|
||||
|
@ -116,19 +116,19 @@ static void event_notify(unsigned int msg_id, unsigned int w_param,
|
|||
(void)w_param;
|
||||
(void)l_param;
|
||||
|
||||
threads_lock_acquire(&dev->mb_lock);
|
||||
thread_lock_acquire(&dev->mb_lock);
|
||||
dev->mailbox |= msg_id;
|
||||
threads_lock_release(&dev->mb_lock);
|
||||
thread_lock_release(&dev->mb_lock);
|
||||
}
|
||||
|
||||
static uint32_t event_fetch(struct tilib_device *dev)
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
threads_lock_acquire(&dev->mb_lock);
|
||||
thread_lock_acquire(&dev->mb_lock);
|
||||
ret = dev->mailbox;
|
||||
dev->mailbox = 0;
|
||||
threads_lock_release(&dev->mb_lock);
|
||||
thread_lock_release(&dev->mb_lock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -497,7 +497,7 @@ static void tilib_destroy(device_t dev_base)
|
|||
printc_dbg("MSP430_Close\n");
|
||||
dev->MSP430_Close(0);
|
||||
dynload_close(dev->hnd);
|
||||
threads_lock_destroy(&dev->mb_lock);
|
||||
thread_lock_destroy(&dev->mb_lock);
|
||||
free(dev);
|
||||
}
|
||||
|
||||
|
@ -631,12 +631,12 @@ static int do_init(struct tilib_device *dev, const struct device_args *args)
|
|||
dev->base.max_breakpoints = DEVICE_MAX_BREAKPOINTS;
|
||||
|
||||
printc_dbg("MSP430_EEM_Init\n");
|
||||
threads_lock_init(&dev->mb_lock);
|
||||
thread_lock_init(&dev->mb_lock);
|
||||
if (dev->MSP430_EEM_Init(event_notify, (long)dev,
|
||||
(MessageID_t *)&my_message_ids) < 0) {
|
||||
report_error(dev, "MSP430_EEM_Init");
|
||||
dev->MSP430_Close(0);
|
||||
threads_lock_destroy(&dev->mb_lock);
|
||||
thread_lock_destroy(&dev->mb_lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,160 @@
|
|||
/* 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. */
|
||||
typedef CONDITION_VARIABLE thread_cond_t;
|
||||
|
||||
static inline void thread_cond_init(thread_cond_t *c)
|
||||
{
|
||||
InitializeConditionVariable(c);
|
||||
}
|
||||
|
||||
static inline void thread_cond_destroy(thread_cond_t *c) { }
|
||||
|
||||
static inline void thread_cond_wait(thread_cond_t *c, thread_lock_t *m)
|
||||
{
|
||||
SleepConditionVariableCS(c, m, INFINITE);
|
||||
}
|
||||
|
||||
static inline void thread_cond_notify(thread_cond_t *c)
|
||||
{
|
||||
WakeConditionVariable(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
|
|
@ -1,77 +0,0 @@
|
|||
/* MSPDebug - debugging tool for MSP430 MCUs
|
||||
* Copyright (C) 2009-2011 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 THREADS_H_
|
||||
#define THREADS_H_
|
||||
|
||||
/* Portable thread utilities interface. */
|
||||
|
||||
#ifdef __Windows__
|
||||
#include <windows.h>
|
||||
|
||||
typedef CRITICAL_SECTION threads_lock_t;
|
||||
|
||||
static inline void threads_lock_init(threads_lock_t *lock)
|
||||
{
|
||||
InitializeCriticalSection(lock);
|
||||
}
|
||||
|
||||
static inline void threads_lock_destroy(threads_lock_t *lock)
|
||||
{
|
||||
DeleteCriticalSection(lock);
|
||||
}
|
||||
|
||||
static inline void threads_lock_acquire(threads_lock_t *lock)
|
||||
{
|
||||
EnterCriticalSection(lock);
|
||||
}
|
||||
|
||||
static inline void threads_lock_release(threads_lock_t *lock)
|
||||
{
|
||||
LeaveCriticalSection(lock);
|
||||
}
|
||||
|
||||
#else /* __Windows__ */
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
typedef pthread_mutex_t threads_lock_t;
|
||||
|
||||
static inline void threads_lock_init(threads_lock_t *lock)
|
||||
{
|
||||
pthread_mutex_init(lock, NULL);
|
||||
}
|
||||
|
||||
static inline void threads_lock_destroy(threads_lock_t *lock)
|
||||
{
|
||||
pthread_mutex_destroy(lock);
|
||||
}
|
||||
|
||||
static inline void threads_lock_acquire(threads_lock_t *lock)
|
||||
{
|
||||
pthread_mutex_lock(lock);
|
||||
}
|
||||
|
||||
static inline void threads_lock_release(threads_lock_t *lock)
|
||||
{
|
||||
pthread_mutex_unlock(lock);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue