medfall

A super great game engine
Log | Files | Refs

locked.h (446B)


      1 #pragma once
      2 
      3 #include "intrinsics.h"
      4 #include "platform_mutex.h"
      5 
      6 template< typename T >
      7 class Locked {
      8 public:
      9 	NONCOPYABLE( Locked );
     10 
     11 	Locked() {
     12 		mutex_init( &mutex );
     13 	}
     14 
     15 	~Locked() {
     16 		mutex_destroy( &mutex );
     17 	}
     18 
     19 	T * acquire() {
     20 		mutex_lock( &mutex );
     21 		return &data;
     22 	}
     23 
     24 	void release() {
     25 		mutex_unlock( &mutex );
     26 	}
     27 
     28 private:
     29 	T data;
     30 	Mutex mutex;
     31 };
     32 
     33 #define SCOPED_ACQUIRE( locked ) locked.acquire(); defer { locked.release(); }