Update wrt-commons_0.2.53
[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 public:
85     /**
86      * decrement the semaphore counter
87      */
88     void Lock() const;
89
90     /**
91      * increment the semaphore counter
92      */
93     void Unlock() const;
94
95      /**
96      * Remove a named semaphore
97      *
98      * @param fileName Name of the semaphore
99      */
100     static void Remove(const std::string &fileName);
101
102     /**
103      * Open an unnamed semaphore
104      *
105      * @param maxLockCount Maximum number of threads allowed to enter semaphore
106      */
107     explicit Semaphore(size_t maxLockCount);
108
109     /**
110      * Open a named semaphore
111      *
112      * @param fileName Semaphore filename
113      * @param allowCreate Should non-existing semaphore be created
114      * @param maxLockCount Maximum number of threads allowed to enter semaphore
115      * @param permissions Semaphore file permissions
116      * @param unlinkOnDestroy Should semaphore file be deleted on destruction
117      */
118     explicit Semaphore(const std::string &fileName,
119                        bool allowCreate = true,
120                        bool exclusiveCreate = false,
121                        size_t maxLockCount = 1,
122                        int permissions = 0600,
123                        bool unlinkOnDestroy = false);
124
125     /**
126      * Destroy a semaphore
127      */
128     ~Semaphore();
129 };
130 } // namespace DPL
131
132 #endif // DPL_SEMAPHORE_H