summaryrefslogtreecommitdiff
path: root/sal/workben/osl/thread/monitor.cxx
blob: 0e5cbefa7c90b77edfbaf0a08db86156b7c61060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/main.h>
#include <osl/mutex.h>
#include <osl/conditn.h>
#include <osl/thread.h>

#include <cstdio>
#include <cassert>

#define BUFFER_SIZE 10

bool queue[9];
sal_uInt32 nItemCount = 0;

oslCondition fullOrEmpty;
oslMutex queueMutex;
oslThread producer, consumer;

void add();
void remove();

void produce(void*);
void consume(void*);

SAL_IMPLEMENT_MAIN()
{
    fprintf(stdout, "Producer/consumer problem - demonstrates mutex, condition variables and threads.\n");

    queueMutex = osl_createMutex();
    fullOrEmpty = osl_createCondition();

    producer = osl_createThread(produce, nullptr);
    consumer = osl_createThread(consume, nullptr);

    osl_joinWithThread(consumer);

    return 0;
}

void produce(void* /* pData */)
{
    osl_setThreadName("producer");

    while(true)
    {
        /* producer monitor - first acquire exclusive access to
           the queue, if the queue is full then wait till there
           is space made available. Once the queue is no longer
           full, then notify that this is the case.
         */
        osl_acquireMutex(queueMutex);

        while (nItemCount == BUFFER_SIZE-1)
        {
            /* we aren't doing anything to the queue, so release
               the mutex and then reacquire it after waiting.
               Without this, we eventually hit a "missed wakeup"
               condition, whereby the thread is sleeping due to
               waiting on the condition variable, but another
               thread preemptively switched right before the
               thread went to sleep and another thread sets the
               condition variable... but of course then it switches
               back to this thread which then waits forever as it
               has missed its wakeup event...
            */
            osl_releaseMutex(queueMutex);
            osl_waitCondition(fullOrEmpty, nullptr);
            osl_acquireMutex(queueMutex);
        }

        fprintf(stdout, "produce()\n");

        add();

        osl_setCondition(fullOrEmpty);
        osl_releaseMutex(queueMutex);

        osl_yieldThread();
    }

    fprintf(stderr, "exit produce()!\n");
}

void consume(void* /* pData */)
{
    osl_setThreadName("consumer");

    while(true)
    {
        /* consumer monitor - first acquire exclusive access to the
           queue, if the queue is empty then wait till something is
           produced. Once the queue is no longer empty, then notify
           that this is the case.
         */
        osl_acquireMutex(queueMutex);

        while (nItemCount == 0)
        {
            /* We aren't doing anything to the queue, so release
               the mutex and then reacquire it after waiting. See
               comments in produce() for the reasoning.
             */
            osl_releaseMutex(queueMutex);
            osl_waitCondition(fullOrEmpty, nullptr);
            osl_acquireMutex(queueMutex);
        }

        fprintf(stdout, "consume()\n");

        remove();

        osl_setCondition(fullOrEmpty);
        osl_releaseMutex(queueMutex);

        osl_yieldThread();
    }

    fprintf(stderr, "exit consume()!\n");
}

void add()
{
    queue[nItemCount] = true;

    fprintf(stdout, "Adding to queue - item %d added.\n", nItemCount);

    nItemCount++;
    assert(nItemCount <= BUFFER_SIZE-1);
}

void remove()
{
    queue[nItemCount] = false;

    fprintf(stdout, "Removing from queue - item %d removed.\n", nItemCount);

    nItemCount--;
    assert(nItemCount >= 0);
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */