Thread: [Assorted-commits] SF.net SVN: assorted: [351] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-10 18:36:30
|
Revision: 351 http://assorted.svn.sourceforge.net/assorted/?rev=351&view=rev Author: yangzhang Date: 2008-02-10 10:36:33 -0800 (Sun, 10 Feb 2008) Log Message: ----------- imported cpuid.h from numa-bench Modified Paths: -------------- cpp-commons/trunk/src/commons/cppcommons.cpp Added Paths: ----------- cpp-commons/trunk/src/commons/cpuid.h Modified: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:28:56 UTC (rev 350) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:36:33 UTC (rev 351) @@ -24,6 +24,7 @@ #endif #include "commons/check.h" +#include "commons/cpuid.h" #include "commons/files.h" #include "commons/strings.h" #include "commons/time.h" Added: cpp-commons/trunk/src/commons/cpuid.h =================================================================== --- cpp-commons/trunk/src/commons/cpuid.h (rev 0) +++ cpp-commons/trunk/src/commons/cpuid.h 2008-02-10 18:36:33 UTC (rev 351) @@ -0,0 +1,71 @@ +// +// This library would be a straightforward target for auto-generation from a +// spec. +// + +#ifndef _COMMONS_CPUID_H +#define _COMMONS_CPUID_H + +namespace commons +{ + + enum { + CACHE = 2, + CACHE_LINE_SIZES = 0x80000005 + }; + +#define cpuid(func,ax,bx,cx,dx)\ + __asm__ __volatile__ ("cpuid":\ + "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func)); + + /** + * Given an extended general-purpose register (e.g. EAX), extract the high + * 8-bit register (AH). + */ + inline unsigned char + high(unsigned int r) + { + return ((r >> 8) & 0xffU); + } + + /** + * Given an extended general-purpose register (e.g. EAX), extract the low + * 8-bit register (AL). + */ + inline unsigned char + low(unsigned int r) + { + return (r & 0xffU); + } + + /** + * Get cache line size in bytes on an Intel CPU. + * References: + * http://softpixel.com/~cwright/programming/simd/cpuid.php + * http://www.intel.com/software/products/documentation/vlin/mergedprojects/analyzer_ec/mergedprojects/reference_olh/mergedprojects/instructions/instruct32_hh/vc46.htm + */ + inline unsigned short + cache_line_sizes_intel() + { + unsigned int a, b, c, d; + cpuid(1, a, b, c, d); + return (unsigned short) (high(b) * 8); + } + + /** + * Get cache line sizes on an AMD CPU. + * Reference: http://softpixel.com/~cwright/programming/simd/cpuid.php + * Maybe look at: http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25481.pdf + */ + inline void + cache_line_sizes_amd(unsigned char *iline, unsigned char *dline) + { + int a, b, c, d; + cpuid(CACHE_LINE_SIZES, a, b, c, d); + *dline = low(c); + *iline = low(d); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-10 18:44:40
|
Revision: 353 http://assorted.svn.sourceforge.net/assorted/?rev=353&view=rev Author: yangzhang Date: 2008-02-10 10:44:43 -0800 (Sun, 10 Feb 2008) Log Message: ----------- added hash functions Modified Paths: -------------- cpp-commons/trunk/src/commons/cppcommons.cpp Added Paths: ----------- cpp-commons/trunk/src/commons/hash.h Modified: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:37:27 UTC (rev 352) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:44:43 UTC (rev 353) @@ -26,6 +26,7 @@ #include "commons/check.h" #include "commons/cpuid.h" #include "commons/files.h" +#include "commons/hash.h" #include "commons/strings.h" #include "commons/time.h" #include "commons/threads.h" Added: cpp-commons/trunk/src/commons/hash.h =================================================================== --- cpp-commons/trunk/src/commons/hash.h (rev 0) +++ cpp-commons/trunk/src/commons/hash.h 2008-02-10 18:44:43 UTC (rev 353) @@ -0,0 +1,45 @@ +#ifndef _COMMONS_HASH_H +#define _COMMONS_HASH_H + +namespace commons +{ + + /** + * From libstdc++ 4.1 __stl_hash_string. + */ + inline size_t + hash_stl(const char* s) + { + unsigned long h = 0; + for ( ; *s; ++s) + h = 5 * h + *s; + return size_t(h); + } + + /** + * From http://www.cse.yorku.ca/~oz/hash.html. + */ + inline size_t + hash_djb2(const char* s) + { + unsigned long h = 5381; + for (; *s; ++s) + h = ((h << 5) + h) + *s; + return size_t(h); + } + + /** + * From Sun JDK6 String.hashCode. + */ + inline size_t + hash_java(const char* s) + { + unsigned long h = 0; + for (; *s; ++s) + h = 31 * h + x; + return size_t(h); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-10 18:52:18
|
Revision: 355 http://assorted.svn.sourceforge.net/assorted/?rev=355&view=rev Author: yangzhang Date: 2008-02-10 10:52:12 -0800 (Sun, 10 Feb 2008) Log Message: ----------- forked x86asm off cpuid; fixed includes Modified Paths: -------------- cpp-commons/trunk/src/commons/cppcommons.cpp cpp-commons/trunk/src/commons/cpuid.h cpp-commons/trunk/src/commons/files.h cpp-commons/trunk/src/commons/hash.h cpp-commons/trunk/src/commons/strings.h Added Paths: ----------- cpp-commons/trunk/src/commons/x86asm.h Modified: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:45:24 UTC (rev 354) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:52:12 UTC (rev 355) @@ -23,11 +23,12 @@ #include <config.h> #endif -#include "commons/check.h" -#include "commons/cpuid.h" -#include "commons/files.h" -#include "commons/hash.h" -#include "commons/strings.h" -#include "commons/time.h" -#include "commons/threads.h" +#include <commons/check.h> +#include <commons/cpuid.h> +#include <commons/files.h> +#include <commons/hash.h> +#include <commons/strings.h> +#include <commons/time.h> +#include <commons/threads.h> +#include <commons/x86asm.h> Modified: cpp-commons/trunk/src/commons/cpuid.h =================================================================== --- cpp-commons/trunk/src/commons/cpuid.h 2008-02-10 18:45:24 UTC (rev 354) +++ cpp-commons/trunk/src/commons/cpuid.h 2008-02-10 18:52:12 UTC (rev 355) @@ -6,6 +6,8 @@ #ifndef _COMMONS_CPUID_H #define _COMMONS_CPUID_H +#include <commons/x86asm.h> + namespace commons { @@ -19,26 +21,6 @@ "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func)); /** - * Given an extended general-purpose register (e.g. EAX), extract the high - * 8-bit register (AH). - */ - inline unsigned char - high(unsigned int r) - { - return ((r >> 8) & 0xffU); - } - - /** - * Given an extended general-purpose register (e.g. EAX), extract the low - * 8-bit register (AL). - */ - inline unsigned char - low(unsigned int r) - { - return (r & 0xffU); - } - - /** * Get cache line size in bytes on an Intel CPU. * References: * http://softpixel.com/~cwright/programming/simd/cpuid.php Modified: cpp-commons/trunk/src/commons/files.h =================================================================== --- cpp-commons/trunk/src/commons/files.h 2008-02-10 18:45:24 UTC (rev 354) +++ cpp-commons/trunk/src/commons/files.h 2008-02-10 18:52:12 UTC (rev 355) @@ -12,7 +12,7 @@ #include <unistd.h> #include <fcntl.h> -#include "commons/check.h" +#include <commons/check.h> namespace commons { Modified: cpp-commons/trunk/src/commons/hash.h =================================================================== --- cpp-commons/trunk/src/commons/hash.h 2008-02-10 18:45:24 UTC (rev 354) +++ cpp-commons/trunk/src/commons/hash.h 2008-02-10 18:52:12 UTC (rev 355) @@ -36,7 +36,7 @@ { unsigned long h = 0; for (; *s; ++s) - h = 31 * h + x; + h = 31 * h + *s; return size_t(h); } Modified: cpp-commons/trunk/src/commons/strings.h =================================================================== --- cpp-commons/trunk/src/commons/strings.h 2008-02-10 18:45:24 UTC (rev 354) +++ cpp-commons/trunk/src/commons/strings.h 2008-02-10 18:52:12 UTC (rev 355) @@ -5,7 +5,7 @@ #include <strings.h> -#include "commons/check.h" +#include <commons/check.h> namespace commons { Added: cpp-commons/trunk/src/commons/x86asm.h =================================================================== --- cpp-commons/trunk/src/commons/x86asm.h (rev 0) +++ cpp-commons/trunk/src/commons/x86asm.h 2008-02-10 18:52:12 UTC (rev 355) @@ -0,0 +1,29 @@ +#ifndef _COMMONS_X86ASM_H +#define _COMMONS_X86ASM_H + +namespace commons +{ + + /** + * Given an extended general-purpose register (e.g. EAX), extract the high + * 8-bit register (AH). + */ + inline unsigned char + high(unsigned int r) + { + return ((r >> 8) & 0xffU); + } + + /** + * Given an extended general-purpose register (e.g. EAX), extract the low + * 8-bit register (AL). + */ + inline unsigned char + low(unsigned int r) + { + return (r & 0xffU); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-11 04:54:01
|
Revision: 360 http://assorted.svn.sourceforge.net/assorted/?rev=360&view=rev Author: yangzhang Date: 2008-02-10 20:54:05 -0800 (Sun, 10 Feb 2008) Log Message: ----------- added boost and boost thread utils Added Paths: ----------- cpp-commons/trunk/src/commons/boost/ cpp-commons/trunk/src/commons/boost/threads.h Added: cpp-commons/trunk/src/commons/boost/threads.h =================================================================== --- cpp-commons/trunk/src/commons/boost/threads.h (rev 0) +++ cpp-commons/trunk/src/commons/boost/threads.h 2008-02-11 04:54:05 UTC (rev 360) @@ -0,0 +1,45 @@ +#ifndef _COMMONS_BOOST_THREADS_H +#define _COMMONS_BOOST_THREADS_H + +#include <pthread.h> +#include <sys/syscall.h> +#include <sys/types.h> +#include <unistd.h> + +#include <boost/function.hpp> + +#include <commons/check.h> +#include <commons/threads.h> + +namespace commons +{ + + using namespace boost; + + /** + * Helper for launching new threads. + */ + void* + run_function0(void* p) + { + const function0<void>* pf = (const function0<void>*) p; + (*pf)(); + delete pf; + 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(const function0<void>& f) + { + pthread_t t; + return pthread_create(&t, NULL, &run_function0, (void*) new function0<void>(f)) == 0 ? t : 0; + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-11 04:54:52
|
Revision: 362 http://assorted.svn.sourceforge.net/assorted/?rev=362&view=rev Author: yangzhang Date: 2008-02-10 20:54:57 -0800 (Sun, 10 Feb 2008) Log Message: ----------- tweaks Modified Paths: -------------- cpp-commons/trunk/src/commons/check.h cpp-commons/trunk/src/commons/cppcommons.cpp cpp-commons/trunk/src/commons/hash.h Modified: cpp-commons/trunk/src/commons/check.h =================================================================== --- cpp-commons/trunk/src/commons/check.h 2008-02-11 04:54:43 UTC (rev 361) +++ cpp-commons/trunk/src/commons/check.h 2008-02-11 04:54:57 UTC (rev 362) @@ -38,6 +38,8 @@ /** * Similar to assert(), but is not conditionally compiled, so this is safe to * use as a guard against expected failures (such as checking return codes). + * This is a macro for two reasons: (1) the file and line, and (2) the lazy + * evaluation of msg. */ #define checkmsg(cond, msg) \ bool b = cond; \ Modified: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-11 04:54:43 UTC (rev 361) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-11 04:54:57 UTC (rev 362) @@ -23,6 +23,7 @@ #include <config.h> #endif +#include <commons/boost/threads.h> #include <commons/check.h> #include <commons/cpuid.h> #include <commons/files.h> Modified: cpp-commons/trunk/src/commons/hash.h =================================================================== --- cpp-commons/trunk/src/commons/hash.h 2008-02-11 04:54:43 UTC (rev 361) +++ cpp-commons/trunk/src/commons/hash.h 2008-02-11 04:54:57 UTC (rev 362) @@ -1,6 +1,8 @@ #ifndef _COMMONS_HASH_H #define _COMMONS_HASH_H +#include <sys/types.h> + namespace commons { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-12 17:21:57
|
Revision: 389 http://assorted.svn.sourceforge.net/assorted/?rev=389&view=rev Author: yangzhang Date: 2008-02-12 08:35:02 -0800 (Tue, 12 Feb 2008) Log Message: ----------- fixed header ifndefs to avoid reserved symbols Modified Paths: -------------- cpp-commons/trunk/src/commons/boost/threads.h cpp-commons/trunk/src/commons/check.h cpp-commons/trunk/src/commons/cpuid.h cpp-commons/trunk/src/commons/deque.h cpp-commons/trunk/src/commons/files.h cpp-commons/trunk/src/commons/hash.h cpp-commons/trunk/src/commons/strings.h cpp-commons/trunk/src/commons/threads.h cpp-commons/trunk/src/commons/time.h cpp-commons/trunk/src/commons/x86asm.h Modified: cpp-commons/trunk/src/commons/boost/threads.h =================================================================== --- cpp-commons/trunk/src/commons/boost/threads.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/boost/threads.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_BOOST_THREADS_H -#define _COMMONS_BOOST_THREADS_H +#ifndef COMMONS_BOOST_THREADS_H +#define COMMONS_BOOST_THREADS_H #include <pthread.h> #include <sys/syscall.h> Modified: cpp-commons/trunk/src/commons/check.h =================================================================== --- cpp-commons/trunk/src/commons/check.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/check.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_CHECK_H -#define _COMMONS_CHECK_H +#ifndef COMMONS_CHECK_H +#define COMMONS_CHECK_H #include <exception> #include <sstream> Modified: cpp-commons/trunk/src/commons/cpuid.h =================================================================== --- cpp-commons/trunk/src/commons/cpuid.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/cpuid.h 2008-02-12 16:35:02 UTC (rev 389) @@ -3,8 +3,8 @@ // spec. // -#ifndef _COMMONS_CPUID_H -#define _COMMONS_CPUID_H +#ifndef COMMONS_CPUID_H +#define COMMONS_CPUID_H #include <commons/x86asm.h> Modified: cpp-commons/trunk/src/commons/deque.h =================================================================== --- cpp-commons/trunk/src/commons/deque.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/deque.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_DEQUE_H -#define _COMMONS_DEQUE_H +#ifndef COMMONS_DEQUE_H +#define COMMONS_DEQUE_H #include <list> #include <vector> Modified: cpp-commons/trunk/src/commons/files.h =================================================================== --- cpp-commons/trunk/src/commons/files.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/files.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_FILES_H -#define _COMMONS_FILES_H +#ifndef COMMONS_FILES_H +#define COMMONS_FILES_H #include <exception> #include <fstream> Modified: cpp-commons/trunk/src/commons/hash.h =================================================================== --- cpp-commons/trunk/src/commons/hash.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/hash.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_HASH_H -#define _COMMONS_HASH_H +#ifndef COMMONS_HASH_H +#define COMMONS_HASH_H #include <sys/types.h> Modified: cpp-commons/trunk/src/commons/strings.h =================================================================== --- cpp-commons/trunk/src/commons/strings.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/strings.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,7 +1,7 @@ // TODO: move whatever you can to C99 Commons -#ifndef _COMMONS_STRINGS_H -#define _COMMONS_STRINGS_H +#ifndef COMMONS_STRINGS_H +#define COMMONS_STRINGS_H #include <strings.h> Modified: cpp-commons/trunk/src/commons/threads.h =================================================================== --- cpp-commons/trunk/src/commons/threads.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/threads.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_THREADS_H -#define _COMMONS_THREADS_H +#ifndef COMMONS_THREADS_H +#define COMMONS_THREADS_H #include <pthread.h> #include <sys/syscall.h> Modified: cpp-commons/trunk/src/commons/time.h =================================================================== --- cpp-commons/trunk/src/commons/time.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/time.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_TIME_H -#define _COMMONS_TIME_H +#ifndef COMMONS_TIME_H +#define COMMONS_TIME_H #include <string> #include <iostream> Modified: cpp-commons/trunk/src/commons/x86asm.h =================================================================== --- cpp-commons/trunk/src/commons/x86asm.h 2008-02-12 16:34:43 UTC (rev 388) +++ cpp-commons/trunk/src/commons/x86asm.h 2008-02-12 16:35:02 UTC (rev 389) @@ -1,5 +1,5 @@ -#ifndef _COMMONS_X86ASM_H -#define _COMMONS_X86ASM_H +#ifndef COMMONS_X86ASM_H +#define COMMONS_X86ASM_H namespace commons { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-12 18:01:23
|
Revision: 393 http://assorted.svn.sourceforge.net/assorted/?rev=393&view=rev Author: yangzhang Date: 2008-02-12 10:01:24 -0800 (Tue, 12 Feb 2008) Log Message: ----------- added some debug output Modified Paths: -------------- cpp-commons/trunk/src/commons/check.h cpp-commons/trunk/src/commons/region.h Modified: cpp-commons/trunk/src/commons/check.h =================================================================== --- cpp-commons/trunk/src/commons/check.h 2008-02-12 18:01:17 UTC (rev 392) +++ cpp-commons/trunk/src/commons/check.h 2008-02-12 18:01:24 UTC (rev 393) @@ -42,8 +42,19 @@ * evaluation of msg. */ #define checkmsg(cond, msg) \ - bool b = cond; \ - if (!b) _check(b, (msg), __FILE__, __LINE__) + bool b__ = cond; \ + if (!b__) _check(b__, (msg), __FILE__, __LINE__) +// TODO: half-written +#define checkmsgf(cond, msg, ...) \ + do { \ + bool b__ = cond; \ + if (!b__) { \ + char s__[4096]; \ + snprintf(s, ); \ + _check(b__, s__, __FILE__, __LINE__); \ + } \ + } while (0) + #endif Modified: cpp-commons/trunk/src/commons/region.h =================================================================== --- cpp-commons/trunk/src/commons/region.h 2008-02-12 18:01:17 UTC (rev 392) +++ cpp-commons/trunk/src/commons/region.h 2008-02-12 18:01:24 UTC (rev 393) @@ -5,6 +5,10 @@ #include <limits> +// XXX: debug +#include <set> +#include <pthread.h> + namespace commons { @@ -39,6 +43,7 @@ const size_t chunk_size; size_t top; int refcount; + set<pthread_t> threads; public: mem_region() : @@ -46,12 +51,13 @@ top(0), refcount(0) { + cout << "created " << this << endl; chunks.push_back((chunk) {chunk_size, new char[chunk_size]}); } ~mem_region() { - // cout << "deleting" << endl; + cout << "deleting" << endl; for (unsigned int i = 0; i < chunks.size(); i++) { delete [] chunks[i].data; } @@ -62,13 +68,15 @@ void *alloc_mem(size_t n) { - if (top + n > chunk_size) { - // chunks.push_back(new char[chunk_size]); - size_t sz = max(n, chunk_size); - chunks.push_back((chunk) {sz, new char[sz] }); + pthread_t t = pthread_self(); + if (threads.insert(t).second) + cout << this << " -- " << t << endl; + + if (top + n > chunks.back().size) { + size_t sz = max(n, chunk_size); + chunks.push_back((chunk) {sz, new char[sz] }); top = 0; } - // cout << "alloced" << endl; size_t old_top = top; top += n; return chunks.back().data + old_top; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-03-12 20:41:51
|
Revision: 621 http://assorted.svn.sourceforge.net/assorted/?rev=621&view=rev Author: yangzhang Date: 2008-03-12 13:41:57 -0700 (Wed, 12 Mar 2008) Log Message: ----------- added minimal pthread barrier implementation (to work around absence in tile64 uclibc) Added Paths: ----------- cpp-commons/trunk/src/commons/pthread/ cpp-commons/trunk/src/commons/pthread/barrier.h Added: cpp-commons/trunk/src/commons/pthread/barrier.h =================================================================== --- cpp-commons/trunk/src/commons/pthread/barrier.h (rev 0) +++ cpp-commons/trunk/src/commons/pthread/barrier.h 2008-03-12 20:41:57 UTC (rev 621) @@ -0,0 +1,47 @@ +#ifndef COMMONS_PTHREAD_BARRIER_H +#define COMMONS_PTHREAD_BARRIER_H + +#include <commons/check.h> +#include <pthread.h> + +#define pthread_barrier_t my_barrier_t +struct my_barrier_t +{ + pthread_mutex_t lock; + pthread_cond_t cond; + int required; + int present; +}; + +int +pthread_barrier_init(pthread_barrier_t* b, pthread_barrierattr_t* a, unsigned count) +{ + check0(pthread_mutex_init(&b->lock, NULL)); + check0(pthread_cond_init (&b->cond, NULL)); + b->required = count; + b->present = 0; + return 0; +} + +int +pthread_barrier_wait(pthread_barrier_t* b) +{ + check0(pthread_mutex_lock(&b->lock)); + if (++b->present == b->required) { + check0(pthread_cond_broadcast(&b->cond)); + } else { + check0(pthread_cond_wait(&b->cond, &b->lock)); + } + check0(pthread_mutex_unlock(&b->lock)); + return 0; +} + +int +pthread_barrier_destroy(pthread_barrier_t* b) +{ + check0(pthread_mutex_destroy(&b->lock)); + check0(pthread_cond_destroy(&b->cond)); + return 0; +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-05-08 19:29:49
|
Revision: 744 http://assorted.svn.sourceforge.net/assorted/?rev=744&view=rev Author: yangzhang Date: 2008-05-08 12:29:35 -0700 (Thu, 08 May 2008) Log Message: ----------- added support for libtask Added Paths: ----------- cpp-commons/trunk/src/commons/task/ cpp-commons/trunk/src/commons/task/net.h cpp-commons/trunk/src/commons/task/task.h Added: cpp-commons/trunk/src/commons/task/net.h =================================================================== --- cpp-commons/trunk/src/commons/task/net.h (rev 0) +++ cpp-commons/trunk/src/commons/task/net.h 2008-05-08 19:29:35 UTC (rev 744) @@ -0,0 +1,97 @@ +#ifndef COMMONS_TASK_NET_H +#define COMMONS_TASK_NET_H + +#include <task.h> + +namespace commons +{ + + /** + * A simple file-descriptor. + */ + class fd + { + private: + int f; + + public: + fd(int f) : f(f) {} + + /** + * Send data. + * \bug Unsafe demotion on \p len. + * \exception check_exception Write failed; not everything was written. + */ + void + write(char *data, size_t len) + { + check(fdwrite(f, data, len) == (int)len); + } + + /** + * Read data. + * \bug Unsafe demotion on \p len. Unsafe demotion on return val. + * \exception check_exception Read failed. + */ + int + read(char *data, size_t len) + { + int res = fdread(f, data, len); + check(res >= 0); + return res; + } + +// /** +// * Read data until \p len. +// * \bug Unsafe demotion on \p len. +// * \exception check_exception Read failed. +// */ +// int +// read(char *data, size_t len) +// { +// while (true) { +// int count = fdread(f, data, len); +// check(count > 0); +// len -= count; +// } +// } + + }; + + typedef fd socket; + + /** + * A TCP listener. + */ + class tcplistener + { + private: + int fd; + + public: + /** + * Bind to the given host and port and start listening. + * \param[in] host If NULL, then bind to all local addresses. + * \param[in] port The port to bind to. + * \exception check_exception Call to netannounce() failed. + */ + tcplistener(char *host, int port) : fd(-1) + { + fd = netannounce(TCP, host, port); + check(fd); + } + + /** + * Accept a connection, ignoring the remote socket address. + * \return The client connection socket. + */ + socket accept() + { + int cli = netaccept(fd, NULL, NULL); + return socket(cli); + } + }; + +} + +#endif Added: cpp-commons/trunk/src/commons/task/task.h =================================================================== --- cpp-commons/trunk/src/commons/task/task.h (rev 0) +++ cpp-commons/trunk/src/commons/task/task.h 2008-05-08 19:29:35 UTC (rev 744) @@ -0,0 +1,21 @@ +#ifndef COMMONS_TASK_H +#define COMMONS_TASK_H + +#include <commons/boost/delegates.h> + +namespace commons +{ + + const unsigned int default_stack = 32768; + + int + spawntask(const function0<void>& f) + { + return taskcreate(&run_function0, + (void*) new function0<void>(f), + default_stack); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-05-08 19:29:52
|
Revision: 745 http://assorted.svn.sourceforge.net/assorted/?rev=745&view=rev Author: yangzhang Date: 2008-05-08 12:29:55 -0700 (Thu, 08 May 2008) Log Message: ----------- added support for state threads (ST) Added Paths: ----------- cpp-commons/trunk/src/commons/st/ cpp-commons/trunk/src/commons/st/st.h Added: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h (rev 0) +++ cpp-commons/trunk/src/commons/st/st.h 2008-05-08 19:29:55 UTC (rev 745) @@ -0,0 +1,118 @@ +#ifndef COMMONS_ST_ST_H +#define COMMONS_ST_ST_H + +#include <st.h> +#include <stx.h> +#include <commons/sockets.h> +#include <commons/boost/delegates.h> +#include <boost/function.hpp> + +namespace commons +{ + using namespace boost; + + enum { default_stack_size = 65536 }; + + /** + * RAII to st_netfd_close() a netfd. + */ + class st_closing + { + public: + st_closing(st_netfd_t x) : attached(true), x(x) {} + ~st_closing() { if (attached) st_netfd_close(x); } + void detach() { attached = false; } + private: + bool attached; + st_netfd_t x; + }; + + /** + * non-copyable. + * \todo remove? this seems to be bad if it closes the file! + */ + class stfd + { + public: + stfd(st_netfd_t fd) : fd_(fd), sclose(fd) {} + const st_netfd_t fd() const { return fd_; } + private: + const st_netfd_t fd_; + st_closing sclose; + }; + + /** + * 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 a pointer? + */ + st_thread_t + st_spawn(const function0<void>& f) + { + return st_thread_create(&run_function0_null, + (void*) new function0<void>(f), + true, + default_stack_size); + } + + /** + * Connect to a TCP socket address. + * \param[in] host Either an IP address or hostname. + * \param[in] port The destination port. + * \param[in] timeout The timeout for each of the DNS lookup and the connect + * operation. + * \todo Create variants that take or return the sockaddr. + */ + st_netfd_t + st_tcp_connect(char *host, int port, st_utime_t timeout) + { + // Create remote socket address. + struct sockaddr_in sa; + bzero(&sa, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + + // First try to parse as IP address. Note: inet_addr() is obsolete. + if (inet_aton(host, &sa.sin_addr) != 0) { + // Then look up by hostname. + check0x(stx_dns_getaddr(host, &sa.sin_addr, timeout)); + } + + // Create the socket. + int sfd = checknneg(socket(PF_INET, SOCK_STREAM, 0)); + st_netfd_t nfd = st_netfd_open_socket(sfd); + + // Connect. + try { + check0x(st_connect(nfd, (struct sockaddr*) &sa, sizeof sa, timeout)); + return nfd; + } catch (...) { + st_netfd_close(nfd); + throw; + } + } + + /** + * Create a listener st_netfd_t. + * \param[in] port The port to listen on. + * \return The st_netfd_t. + */ + st_netfd_t + st_tcp_listen(int port) + { + int sfd = tcp_listen(port); + try { + // Create a net file descriptor around a listener socket. + st_netfd_t nfd = st_netfd_open_socket(sfd); + checkpass(nfd); + //st_netfd_t nfd = checkpass(st_netfd_open_socket(sfd)); + return nfd; + } catch (...) { + close(sfd); + throw; + } + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-05-08 19:32:40
|
Revision: 748 http://assorted.svn.sourceforge.net/assorted/?rev=748&view=rev Author: yangzhang Date: 2008-05-08 12:32:41 -0700 (Thu, 08 May 2008) Log Message: ----------- added closing RAII tool and die() Added Paths: ----------- cpp-commons/trunk/src/commons/closing.h cpp-commons/trunk/src/commons/die.h Added: cpp-commons/trunk/src/commons/closing.h =================================================================== --- cpp-commons/trunk/src/commons/closing.h (rev 0) +++ cpp-commons/trunk/src/commons/closing.h 2008-05-08 19:32:41 UTC (rev 748) @@ -0,0 +1,19 @@ +#ifndef COMMONS_CLOSING_H +#define COMMONS_CLOSING_H + +namespace +{ + + template<typename T> + class closing + { + public: + closing(T x) : x(x) {} + ~closing() { close(x); } + private: + T x; + }; + +} + +#endif Added: cpp-commons/trunk/src/commons/die.h =================================================================== --- cpp-commons/trunk/src/commons/die.h (rev 0) +++ cpp-commons/trunk/src/commons/die.h 2008-05-08 19:32:41 UTC (rev 748) @@ -0,0 +1,33 @@ +#ifndef COMMONS_DIE_H +#define COMMONS_DIE_H + +#include <cerrno> +#include <cstdio> +#include <cstdarg> +#include <cstring> +#include <cstdlib> + +namespace commons { + + /** + * Print an error message and exit. Acts like perror() if \p format doesn't + * end with "\n". + * + * TODO: move into C Commons. + */ + void + die(const char *format, ...) + { + const char *errstr; + va_list val; + va_start(val, format); + errstr = strerror(errno); + vfprintf(stderr, format, val); + if (strchr(format, '\n') == NULL) + fprintf(stderr, ": %s\n", errstr); + exit(1); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |