blob: ac56a0c266ac286e685298ac15a14aa68b8e4589 [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * Copyright (c) 2019, Infosys Ltd.
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#include <bits/stdc++.h>
18#include <iostream>
19#include <sys/ipc.h>
20#include <sys/shm.h>
21#include "procedureQueue.h"
22#include "controlBlock.h"
23
24namespace SM
25{
26const size_t PROC_QUE_MAX_SIZE = 1000;
27
28ProcedureQueue::ProcedureQueue():cbQ(), mutex_m()
29{
30 sem_init(&pop_semaphore, 0, 0);
31}
32
33ProcedureQueue::~ProcedureQueue()
34{
35 queue<ControlBlock*> q;
36 swap( cbQ, q);
37 sem_destroy(&pop_semaphore);
38}
39
40ControlBlock* ProcedureQueue::pop()
41{
42 sem_wait(&pop_semaphore);
43
44 std::lock_guard<std::mutex> lock(mutex_m);
45
46 ControlBlock* cb = NULL;
47
48 if(cbQ.empty())
49 {
50 return NULL;
51 }
52
53 cb = cbQ.front();
54 cbQ.pop();
55
56 return cb;
57}
58
59bool ProcedureQueue::push(ControlBlock* cb)
60{
61 std::lock_guard<std::mutex> lock(mutex_m);
62
63 if(PROC_QUE_MAX_SIZE == cbQ.size())
64 {
65 return false;
66 }
67
68 cbQ.push(cb);
69 sem_post(&pop_semaphore);
70
71 return true;
72}
73}