Thread: [Assorted-commits] SF.net SVN: assorted: [361] cpp-commons/trunk/src/commons/threads.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-11 04:54:38
|
Revision: 361 http://assorted.svn.sourceforge.net/assorted/?rev=361&view=rev Author: yangzhang Date: 2008-02-10 20:54:43 -0800 (Sun, 10 Feb 2008) Log Message: ----------- cleaned up threads.h; added spawn, runner Modified Paths: -------------- cpp-commons/trunk/src/commons/threads.h Modified: cpp-commons/trunk/src/commons/threads.h =================================================================== --- cpp-commons/trunk/src/commons/threads.h 2008-02-11 04:54:05 UTC (rev 360) +++ cpp-commons/trunk/src/commons/threads.h 2008-02-11 04:54:43 UTC (rev 361) @@ -1,133 +1,193 @@ -// TODO: use boost::bind? - #ifndef _COMMONS_THREADS_H #define _COMMONS_THREADS_H #include <pthread.h> +#include <sys/syscall.h> +#include <sys/types.h> +#include <unistd.h> +#include <commons/check.h> + namespace commons { - // non-rpc-specific utility to start a thread that runs - // an object method. returns a pthread_t on success, and - // zero on error. + /** + * Get the current thread ID. Glibc does not provide a wrapper for this + * system call. + */ + inline pid_t + gettid() + { + return (pid_t) syscall(SYS_gettid); + } + + /** + * Wait for all the given threads to join, discarding their return values. + */ + inline void + waitall(pthread_t* ts, int n) + { + for (int i = 0; i < n; i++) { + check(pthread_join(ts[i], NULL) == 0); + } + } + + /** + * Helper for launching new threads. + */ + void* + runner(void *p) + { + void (*f)() = (void(*)()) p; + (*f)(); + return NULL; + } + + /** + * Run a function in pthread. + * \return The new pthread_t on success, 0 on failure. + * TODO: Is it safe to treat the pthread_t as an integral type? + */ + pthread_t + spawn(void (*f)()) + { + pthread_t t; + return pthread_create(&t, NULL, &runner, (void*) f) == 0 ? t : 0; + } + + /** + * Run a method in pthread. + * \return The new pthread_t on success, 0 on failure. + */ template <class C> pthread_t method_thread(C *o, void (C::*m)()) { - class XXX { + class runnable { public: C *o; void (C::*m)(); - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)() = x->m; - delete x; + static void *run(void *vvv) { + runnable *r = (runnable*)vvv; + C *o = r->o; + void (C::*m)() = r->m; + delete r; (o->*m)(); return 0; } }; - XXX *x = new XXX; - x->o = o; - x->m = m; + runnable *r = new runnable; + r->o = o; + r->m = m; pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + if(pthread_create(&th, NULL, &runnable::run, (void *) r) == 0){ return th; } return 0; } + /** + * Run a method in pthread. + * \return The new pthread_t on success, 0 on failure. + */ template <class C, class A> pthread_t method_thread(C *o, void (C::*m)(A), A a) { - class XXX { + class runnable { public: C *o; void (C::*m)(A a); A a; - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)(A ) = x->m; - A a = x->a; - delete x; + static void *run(void *vvv) { + runnable *r = (runnable*)vvv; + C *o = r->o; + void (C::*m)(A ) = r->m; + A a = r->a; + delete r; (o->*m)(a); return 0; } }; - XXX *x = new XXX; - x->o = o; - x->m = m; - x->a = a; + runnable *r = new runnable; + r->o = o; + r->m = m; + r->a = a; pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + if(pthread_create(&th, NULL, &runnable::run, (void *) r) == 0){ return th; } return 0; } + /** + * Run a method in pthread. + * \return The new pthread_t on success, 0 on failure. + */ template <class C, class A1, class A2> pthread_t method_thread(C *o, void (C::*m)(A1 , A2 ), A1 a1, A2 a2) { - class XXX { + class runnable { public: C *o; void (C::*m)(A1 a1, A2 a2); A1 a1; - A2 a2; - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)(A1 , A2 ) = x->m; - A1 a1 = x->a1; - A2 a2 = x->a2; - delete x; + A2 a2; + static void *run(void *vvv) { + runnable *r = (runnable*)vvv; + C *o = r->o; + void (C::*m)(A1 , A2 ) = r->m; + A1 a1 = r->a1; + A2 a2 = r->a2; + delete r; (o->*m)(a1, a2); return 0; } }; - XXX *x = new XXX; - x->o = o; - x->m = m; - x->a1 = a1; - x->a2 = a2; + runnable *r = new runnable; + r->o = o; + r->m = m; + r->a1 = a1; + r->a2 = a2; pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + if(pthread_create(&th, NULL, &runnable::run, (void *) r) == 0){ return th; } return 0; } + /** + * Run a method in pthread. + * \return The new pthread_t on success, 0 on failure. + */ template <class C, class A1, class A2, class A3> pthread_t method_thread(C *o, void (C::*m)(A1 , A2, A3), A1 a1, A2 a2, A3 a3) { - class XXX { + class runnable { public: C *o; void (C::*m)(A1 a1, A2 a2, A3 a3); A1 a1; A2 a2; A3 a3; - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)(A1, A2, A3) = x->m; - A1 a1 = x->a1; - A2 a2 = x->a2; - A3 a3 = x->a3; - delete x; + static void *run(void *vvv) { + runnable *r = (runnable*)vvv; + C *o = r->o; + void (C::*m)(A1, A2, A3) = r->m; + A1 a1 = r->a1; + A2 a2 = r->a2; + A3 a3 = r->a3; + delete r; (o->*m)(a1, a2, a3); return 0; } }; - XXX *x = new XXX; - x->o = o; - x->m = m; - x->a1 = a1; - x->a2 = a2; - x->a3 = a3; + runnable *r = new runnable; + r->o = o; + r->m = m; + r->a1 = a1; + r->a2 = a2; + r->a3 = a3; pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + if(pthread_create(&th, NULL, &runnable::run, (void *) r) == 0){ return th; } return 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-14 00:10:05
|
Revision: 410 http://assorted.svn.sourceforge.net/assorted/?rev=410&view=rev Author: yangzhang Date: 2008-02-13 16:10:10 -0800 (Wed, 13 Feb 2008) Log Message: ----------- added pin_thread Modified Paths: -------------- cpp-commons/trunk/src/commons/threads.h Modified: cpp-commons/trunk/src/commons/threads.h =================================================================== --- cpp-commons/trunk/src/commons/threads.h 2008-02-13 18:03:22 UTC (rev 409) +++ cpp-commons/trunk/src/commons/threads.h 2008-02-14 00:10:10 UTC (rev 410) @@ -22,6 +22,19 @@ } /** + * Pin the thread to the given CPU number (as defined for sched_setaffinity). + */ + void + pin_thread(int cpu) + { + pid_t pid = gettid(); + cpu_set_t cs; + CPU_ZERO(&cs); + CPU_SET(cpu, &cs); + sched_setaffinity(pid, sizeof(cs), &cs); + } + + /** * Wait for all the given threads to join, discarding their return values. */ inline void This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |