Initialize Tizen 2.3
[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 class Semaphore :
32     private Noncopyable
33 {
34   public:
35     class ScopedLock :
36         private Noncopyable
37     {
38       private:
39         Semaphore *m_semaphore;
40
41       public:
42         explicit ScopedLock(Semaphore *semaphore);
43         ~ScopedLock();
44     };
45
46     class Exception
47     {
48       public:
49         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
50         DECLARE_EXCEPTION_TYPE(Base, CreateFailed)
51         DECLARE_EXCEPTION_TYPE(Base, LockFailed)
52         DECLARE_EXCEPTION_TYPE(Base, UnlockFailed)
53         DECLARE_EXCEPTION_TYPE(Base, RemoveFailed)
54     };
55
56   private:
57     enum Type
58     {
59         Type_Unnamed,
60         Type_Named
61     };
62
63     Type m_type;
64
65     mutable union
66     {
67         struct
68         {
69             sem_t handle;
70         } unnamed;
71
72         struct
73         {
74             sem_t *handle;
75             char *name;
76             bool unlinkOnDestroy;
77         } named;
78     } m_semaphore;
79
80     sem_t *InternalGet() const;
81     void InternalDestroy();
82
83   public:
84     /**
85      * decrement the semaphore counter
86      */
87     void Lock() const;
88
89     /**
90      * increment the semaphore counter
91      */
92     void Unlock() const;
93
94     /**
95      * Remove a named semaphore
96      *
97      * @param fileName Name of the semaphore
98      */
99     static void Remove(const std::string &fileName);
100
101     /**
102      * Open an unnamed semaphore
103      *
104      * @param maxLockCount Maximum number of threads allowed to enter semaphore
105      */
106     explicit Semaphore(size_t maxLockCount);
107
108     /**
109      * Open a named semaphore
110      *
111      * @param fileName Semaphore filename
112      * @param allowCreate Should non-existing semaphore be created
113      * @param maxLockCount Maximum number of threads allowed to enter semaphore
114      * @param permissions Semaphore file permissions
115      * @param unlinkOnDestroy Should semaphore file be deleted on destruction
116      */
117     explicit Semaphore(const std::string &fileName,
118                        bool allowCreate = true,
119                        bool exclusiveCreate = false,
120                        size_t maxLockCount = 1,
121                        int permissions = 0600,
122                        bool unlinkOnDestroy = false);
123
124     /**
125      * Destroy a semaphore
126      */
127     ~Semaphore();
128 };
129 } // namespace DPL
130
131 #endif // DPL_SEMAPHORE_H