OSDN Git Service

Add new file.
[ultramonkey-l7/ultramonkey-l7-v3.git] / lib / thread / include / spin_mutex.h
1 #ifndef SPIN_MUTEX_H
2 #define SPIN_MUTEX_H
3 //
4 // (C) Copyright 2009- Norihisa Nakai
5 // Distributed under the Boost Software License, Version 1.0
6 // (See accompanying file LICENSE_1_0.txt http://www.boost.org/LICENSE_1_0.txt)
7
8
9 #include <boost/noncopyable.hpp>
10 #include <boost/thread/exceptions.hpp>
11 #include <error.h>
12 #include <pthread.h>
13
14 namespace ultramonkey{
15
16 class   spin_mutex : boost::noncopyable{
17 protected:
18   pthread_spinlock_t    m;
19 public:
20   spin_mutex(){
21         int const res = pthread_spin_init( &m, NULL );
22         if( res ) throw boost::thread_resource_error();
23   }
24
25   ~spin_mutex(){
26         pthread_spin_destroy(&m);
27   }
28
29   void  lock(){ pthread_spin_lock(&m); }
30   void  unlock(){ pthread_spin_unlock(&m); }
31   bool  try_lock(){
32         int const res = pthread_spin_trylock(&m);
33         return !res;
34   }
35 };
36
37 }       // namespace ultramonkey
38 #endif  //SPIN_MUTEX_H