tizen beta release
[framework/web/wrt-commons.git] / modules / core / include / dpl / semaphore.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        semaphore.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of semaphore
21  */
22 #ifndef DPL_SEMAPHORE_H
23 #define DPL_SEMAPHORE_H
24
25 #include <dpl/noncopyable.h>
26 #include <dpl/exception.h>
27 #include <semaphore.h>
28 #include <string>
29
30 namespace DPL
31 {
32 class Semaphore
33     : private Noncopyable
34 {
35 public:
36     class ScopedLock
37         : private Noncopyable
38     {
39     private:
40         Semaphore *m_semaphore;
41
42     public:
43         explicit ScopedLock(Semaphore *semaphore);
44         ~ScopedLock();
45     };
46
47     class Exception
48     {
49     public:
50         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
51         DECLARE_EXCEPTION_TYPE(Base, CreateFailed)
52         DECLARE_EXCEPTION_TYPE(Base, LockFailed)
53         DECLARE_EXCEPTION_TYPE(Base, UnlockFailed)
54         DECLARE_EXCEPTION_TYPE(Base, RemoveFailed)
55     };
56
57 private:
58     enum Type
59     {
60         Type_Unnamed,
61         Type_Named
62     };
63
64     Type m_type;
65
66     mutable union
67     {
68         struct
69         {
70             sem_t handle;
71         } unnamed;
72
73         struct
74         {
75             sem_t *handle;
76             char *name;
77             bool unlinkOnDestroy;
78         } named;
79     } m_semaphore;
80
81     sem_t *InternalGet() const;
82     void InternalDestroy();
83
84     void Lock() const;
85     void Unlock() const;
86
87 public:
88     /**
89      * Remove a named semaphore
90      *
91      * @param fileName Name of the semaphore
92      */
93     static void Remove(const std::string &fileName);
94
95     /**
96      * Open an unnamed semaphore
97      *
98      * @param maxLockCount Maximum number of threads allowed to enter semaphore
99      */
100     explicit Semaphore(size_t maxLockCount);
101
102     /**
103      * Open a named semaphore
104      *
105      * @param fileName Semaphore filename
106      * @param allowCreate Should non-existing semaphore be created
107      * @param maxLockCount Maximum number of threads allowed to enter semaphore
108      * @param permissions Semaphore file permissions
109      * @param unlinkOnDestroy Should semaphore file be deleted on destruction
110      */
111     explicit Semaphore(const std::string &fileName,
112                        bool allowCreate = true,
113                        bool exclusiveCreate = false,
114                        size_t maxLockCount = 1,
115                        int permissions = 0600,
116                        bool unlinkOnDestroy = false);
117
118     /**
119      * Destroy a semaphore
120      */
121     ~Semaphore();
122 };
123 } // namespace DPL
124
125 #endif // DPL_SEMAPHORE_H