ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.65 by root, Tue Apr 1 19:50:38 2008 UTC vs.
Revision 1.66 by root, Wed Apr 2 11:13:55 2008 UTC

43// is mostly true or mosty false. note that these return 43// is mostly true or mosty false. note that these return
44// booleans, not the expression. 44// booleans, not the expression.
45#define expect_false(expr) expect ((expr) != 0, 0) 45#define expect_false(expr) expect ((expr) != 0, 0)
46#define expect_true(expr) expect ((expr) != 0, 1) 46#define expect_true(expr) expect ((expr) != 0, 1)
47 47
48#include <pthread.h>
49
48#include <cstddef> 50#include <cstddef>
49#include <cmath> 51#include <cmath>
50#include <new> 52#include <new>
51#include <vector> 53#include <vector>
52 54
80 82
81// in range excluding end 83// in range excluding end
82#define IN_RANGE_EXC(val,beg,end) \ 84#define IN_RANGE_EXC(val,beg,end) \
83 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 85 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
84 86
87void cleanup (const char *cause, bool make_core = false);
85void fork_abort (const char *msg); 88void fork_abort (const char *msg);
86 89
87// rationale for using (U) not (T) is to reduce signed/unsigned issues, 90// rationale for using (U) not (T) is to reduce signed/unsigned issues,
88// as a is often a constant while b is the variable. it is still a bug, though. 91// as a is often a constant while b is the variable. it is still a bug, though.
89template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } 92template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
568int similar_direction (int a, int b); 571int similar_direction (int a, int b);
569 572
570// like sprintf, but returns a "static" buffer 573// like sprintf, but returns a "static" buffer
571const char *format (const char *format, ...); 574const char *format (const char *format, ...);
572 575
573#endif 576/////////////////////////////////////////////////////////////////////////////
577// threads, very very thin wrappers around pthreads
574 578
579struct thread
580{
581 pthread_t id;
582
583 void start (void *(*start_routine)(void *), void *arg = 0);
584
585 void cancel ()
586 {
587 pthread_cancel (id);
588 }
589
590 void *join ()
591 {
592 void *ret;
593
594 if (pthread_join (id, &ret))
595 cleanup ("pthread_join failed", 1);
596
597 return ret;
598 }
599};
600
601// note that mutexes are not classes
602typedef pthread_mutex_t smutex;
603
604#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
605 #define SMUTEX_INITIALISER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
606#else
607 #define SMUTEX_INITIALISER PTHREAD_MUTEX_INITIALIZER
608#endif
609
610#define SMUTEX(name) smutex name = SMUTEX_INITIALISER
611#define SMUTEX_LOCK(name) pthread_mutex_lock (&(name))
612#define SMUTEX_UNLOCK(name) pthread_mutex_unlock (&(name))
613
614#endif
615

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines