DPL: work around for dependency on DPL::Thread
[platform/core/security/security-manager.git] / src / server / dpl / db / src / naive_synchronization_object.cpp
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        naive_synchronization_object.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of SQL naive
21  * synchronization object
22  */
23 #include <stddef.h>
24 #include <dpl/db/naive_synchronization_object.h>
25 #include <dpl/assert.h>
26 #include <time.h>
27
28 namespace {
29     unsigned int seed = time(NULL);
30 }
31
32 //Taken from Thread class, so we don't have to pull whole definition
33 //(only this part is needed)
34 namespace Thread {
35
36 static const size_t NANOSECONDS_PER_SECOND =
37     static_cast<uint64_t>(1000 * 1000 * 1000);
38
39 static const size_t NANOSECONDS_PER_MILISECOND =
40     static_cast<uint64_t>(1000 * 1000);
41
42 void NanoSleep(uint64_t nanoseconds)
43 {
44     timespec requestedTime = {
45         static_cast<time_t>(
46             nanoseconds / NANOSECONDS_PER_SECOND),
47
48         static_cast<long>(
49             nanoseconds % NANOSECONDS_PER_SECOND)
50     };
51
52     timespec remainingTime;
53
54     for (;;) {
55         if (nanosleep(&requestedTime, &remainingTime) == 0) {
56             break;
57         }
58
59         int error = errno;
60         Assert(error == EINTR);
61
62         requestedTime = remainingTime;
63     }
64 }
65
66 void MiliSleep(uint64_t miliseconds)
67 {
68     NanoSleep(miliseconds * NANOSECONDS_PER_MILISECOND);
69 }
70 }
71
72 namespace DPL {
73 namespace DB {
74 void NaiveSynchronizationObject::Synchronize()
75 {
76     // Sleep for about 10ms - 30ms
77     Thread::MiliSleep(10 + rand_r(&seed) % 20);
78 }
79
80 void NaiveSynchronizationObject::NotifyAll()
81 {
82     // No need to inform about anything
83 }
84 } // namespace DB
85 } // namespace DPL