threadsafe.cpp

Go to the documentation of this file.
00001 /*
00002  * threadsafe.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "threadsafe.h"         // always include our own header first!
00035 
00036 #include "common/wave_ex.h"
00037 #include "perf/perf.h"
00038 #include "threadsafe/smart_mutex.h"
00039 
00040 
00041 
00042 ////////////////////////////////////////////////////////////////////////////////
00043 //
00044 //      static helper methods
00045 //
00046 ////////////////////////////////////////////////////////////////////////////////
00047 
00048 #ifdef WIN32
00049 
00050 struct win32_context_t {
00051         // constructor, manipulators
00052         win32_context_t(void) throw() { this->clear(); }
00053         void clear(void) throw() {
00054                         start_fn = NULL;
00055                         real_context = NULL;
00056                 }
00057 
00058         // data fields
00059         thread_start_fn_t       start_fn;
00060         void *                  real_context;
00061 };
00062 
00063 
00064 static DWORD WINAPI
00065 win32start
00066 (
00067 IN LPVOID ctx
00068 )
00069 {
00070         win32_context_t * win32ctx = (win32_context_t *) ctx;
00071         ASSERT(win32ctx, "should always have win32 thread context");
00072         ASSERT(win32ctx->start_fn, "null thread start function");
00073 
00074         win32ctx->start_fn(win32ctx->real_context);
00075 
00076         return 0;
00077 }
00078 
00079 
00080 #endif  // WIN32
00081 
00082 
00083 ////////////////////////////////////////////////////////////////////////////////
00084 //
00085 //      public API
00086 //
00087 ////////////////////////////////////////////////////////////////////////////////
00088 
00089 int
00090 createThread
00091 (
00092 IN thread_start_fn_t start_fn,
00093 IN void * context,
00094 OUT thread_id_t& threadId
00095 )
00096 {
00097         ASSERT(start_fn, "null thread start function");
00098         // ASSERT(context) -- we don't care!
00099         threadId = 0;
00100 
00101 #ifdef WIN32
00102 // win32 threading implementation
00103         ASSERT(sizeof(thread_id_t) >= sizeof(dword_t),
00104             "thread_id_t type is too small!");
00105         dword_t stackSize = 0;  // use default
00106         dword_t flags = 0;      // default flags
00107         win32_context_t wctx;
00108         wctx.start_fn = start_fn;
00109         wctx.real_context = context;
00110         dword_t dwThreadId = 0;
00111         HANDLE hThread =
00112             CreateThread(NULL, stackSize, win32start, &wctx, flags,
00113                 (LPDWORD) &dwThreadId);
00114         if (!hThread) {
00115                 int error = GetLastError();
00116                 DPRINTF("Failed to create new thread: %d", error);
00117                 return (error) ? error : 1;
00118         }
00119         threadId = (thread_id_t) dwThreadId;
00120 
00121 #else   // WIN32
00122 // posix threading implementation
00123 
00124         ASSERT(sizeof(thread_id_t) >= sizeof(pthread_t),
00125             "thread_id_t type is too small!");
00126         pthread_t pid;
00127         pthread_create(&pid, NULL, start_fn, context);
00128         threadId = (thread_id_t) pid;
00129 
00130 #endif  // WIN32
00131 
00132         // must be okay!
00133         return 0;
00134 }
00135 
00136 
00137 
00138 thread_id_t
00139 getCurrentThreadId
00140 (
00141 void
00142 )
00143 {
00144 #ifdef WIN32
00145         return (thread_id_t) GetCurrentThreadId();
00146 #else   // WIN32
00147         return (thread_id_t) pthread_self();
00148 #endif  // WIN32
00149 }
00150 
00151 
00152 
00153 void
00154 sleepMilliseconds
00155 (
00156 IN long milliseconds
00157 )
00158 {
00159         ASSERT(milliseconds >= 0, "bad sleep milliseconds: %d",
00160             (int) milliseconds);
00161 #ifdef WIN32
00162         // windows: use Sleep()
00163         Sleep(milliseconds);
00164 #else   // WIN32
00165         // linux/posix: use nanosleep
00166         struct timespec ts;
00167         ts.tv_sec = 0;
00168         ts.tv_nsec = 1000 * 1000 * milliseconds;
00169         nanosleep(&ts, NULL);
00170 #endif  // WIN32
00171 }
00172