diff --git a/checkin_notes b/checkin_notes index 700da5f09f..aa97a8e9d7 100644 --- a/checkin_notes +++ b/checkin_notes @@ -6219,3 +6219,9 @@ David 18 Sept 2011 parse.cpp win_build/ boinc_cli.vcproj + +David 18 Sept 2011 + - lib: add generic interfaces for threads and thread synchronization + + lib/ + thread.cpp,h diff --git a/lib/Makefile.am b/lib/Makefile.am index 18defb25de..b18b791897 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -61,6 +61,7 @@ generic_sources = \ shmem.cpp \ str_util.cpp \ synch.cpp \ + thread.cpp \ unix_util.cpp \ url.cpp \ util.cpp diff --git a/lib/thread.cpp b/lib/thread.cpp new file mode 100644 index 0000000000..45c587e3c2 --- /dev/null +++ b/lib/thread.cpp @@ -0,0 +1,62 @@ +// This file is part of BOINC. +// http://boinc.berkeley.edu +// Copyright (C) 2011 University of California +// +// BOINC is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License +// as published by the Free Software Foundation, +// either version 3 of the License, or (at your option) any later version. +// +// BOINC 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with BOINC. If not, see . + +#ifdef _WIN32 +#else +#endif + +#include "thread.h" + +int THREAD::run(void*(*func)(void*), void* _arg) { +#ifdef _WIN32 + CreateThread(NULL, 0, func, 0, 0, NULL); +#else + pthread_t id; + pthread_attr_t thread_attrs; + pthread_attr_init(&thread_attrs); + pthread_create(&id, &thread_attrs, func, NULL); +#endif + return 0; +} + +void THREAD::quit() { + quit_flag = true; +} + +THREAD_LOCK::THREAD_LOCK() { +#ifdef _WIN32 + InitializeCriticalSection(&mutex); +#else + pthread_mutex_init(&mutex, NULL); +#endif +} + +void THREAD_LOCK::lock() { +#ifdef _WIN32 + EnterCriticalSection(&mutex); +#else + pthread_mutex_lock(&mutex); +#endif +} + +void THREAD_LOCK::unlock() { +#ifdef _WIN32 + LeaveCriticalSection(&mutex); +#else + pthread_mutex_lock(&mutex); +#endif +} diff --git a/lib/thread.h b/lib/thread.h new file mode 100644 index 0000000000..05e7a7798e --- /dev/null +++ b/lib/thread.h @@ -0,0 +1,40 @@ +// This file is part of BOINC. +// http://boinc.berkeley.edu +// Copyright (C) 2011 University of California +// +// BOINC is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License +// as published by the Free Software Foundation, +// either version 3 of the License, or (at your option) any later version. +// +// BOINC 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with BOINC. If not, see . + +#ifdef _WIN32 +#else +#include +#endif + +struct THREAD { + void* arg; + bool quit_flag; + int run(void*(*func)(void*), void*); + void quit(); +}; + +struct THREAD_LOCK { +#ifdef _WIN32 + CRITICAL_SECTION mutex; +#else + pthread_mutex_t mutex; +#endif + void lock(); + void unlock(); + + THREAD_LOCK(); +};