00001
00002
00003
00004
00005
00006 #ifndef __RW_LOCK_IO_COMPL_H_Included__
00007 #define __RW_LOCK_IO_COMPL_H_Included__
00008
00009 #include <Windows.h>
00010 #include "atomic_util.h"
00011 #include "system_abstraction.h"
00012
00013 class NonblockingRWLockIOCompl
00014 {
00015 public:
00016 static const char *name() { return "NonblockingRWLockIOCompl"; }
00017
00018 inline void take_writer_lock(void)
00019 {
00020 DWORD ln;
00021 ULONG_PTR key;
00022 LPOVERLAPPED ovlp;
00023
00024 while(!GetQueuedCompletionStatus(
00025 writers_iocport,
00026 &ln,
00027 &key,
00028 &ovlp,
00029 200))
00030 {
00031 thread_yield();
00032 }
00033 atomic_dec(&lock_tokens);
00034
00035 }
00036
00037 inline void release_writer_lock(void)
00038 {
00039 assert(PostQueuedCompletionStatus(
00040 writers_iocport,
00041 0,
00042 NULL,
00043 NULL)!=0);
00044 atomic_inc(&lock_tokens);
00045 }
00046 NonblockingRWLockIOCompl() : readers(0L), writers(0L), lock_tokens(0)
00047 {
00048 writers_iocport = CreateIoCompletionPort(
00049 INVALID_HANDLE_VALUE,
00050 NULL,
00051 NULL,
00052 0);
00053 release_writer_lock();
00054 }
00055
00056 virtual ~NonblockingRWLockIOCompl()
00057 {
00058 CloseHandle(writers_iocport);
00059 }
00060
00061
00062
00063 inline void enter_read(void)
00064 {
00065 atomic_inc(&readers);
00066 while(writers)
00067 {
00068 atomic_dec(&readers);
00069 take_writer_lock();
00070 atomic_inc(&readers);
00071 release_writer_lock();
00072 }
00073 }
00074
00075
00076
00077
00078 inline void leave_read(void)
00079 {
00080 atomic_dec(&readers);
00081 }
00082
00083
00084
00085
00086 inline void enter_write(void)
00087 {
00088 take_writer_lock();
00089 atomic_inc(&writers);
00090 while(readers > 0L)
00091 {
00092 yield();
00093 }
00094 }
00095
00096
00097
00098
00099 inline void leave_write(void)
00100 {
00101 atomic_dec(&writers);
00102 release_writer_lock();
00103 }
00104
00105 private:
00106 int readers;
00107 int writers;
00108 int lock_tokens;
00109 HANDLE writers_iocport;
00110 };
00111
00112 #endif // __RW_LOCK_IO_COMPL_H_Included__
00113
00114
00115