ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

53 lines
922 B

  1. #ifndef CPPCMS_FCNTL_MUTEX_H
  2. #define CPPCMS_FCNTL_MUTEX_H
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. namespace cppcms {
  6. namespace {
  7. void do_lock(FILE *f,int how,bool do_throw=false)
  8. {
  9. struct flock lock = {0};
  10. lock.l_type=how;
  11. lock.l_start=0;
  12. lock.l_len=0;
  13. lock.l_whence=SEEK_SET;
  14. int res;
  15. while((res=::fcntl(fileno(f),F_SETLKW,&lock))<0 && errno==EINTR)
  16. ;
  17. if(res!=0 && do_throw)
  18. throw cppcms_error(errno,"fcntl failed");
  19. }
  20. }
  21. namespace fcntl {
  22. class mutex_lock {
  23. FILE *m;
  24. public:
  25. mutex_lock(FILE *p): m(p) { do_lock(m,F_WRLCK,true); };
  26. ~mutex_lock() { do_lock(m,F_UNLCK); }
  27. };
  28. class rwlock_rdlock {
  29. FILE *m;
  30. public:
  31. rwlock_rdlock(FILE *p): m(p) { do_lock(m,F_RDLCK,true); };
  32. ~rwlock_rdlock() { do_lock(m,F_UNLCK); };
  33. };
  34. class rwlock_wrlock {
  35. FILE *m;
  36. public:
  37. rwlock_wrlock(FILE *p): m(p) { do_lock(m,F_WRLCK,true); };
  38. ~rwlock_wrlock() { do_lock(m,F_UNLCK); };
  39. };
  40. }
  41. } // cppcms
  42. #endif