summaryrefslogtreecommitdiffstats
path: root/src/base/test/thread.cpp
blob: ab327ff7156e365f10c7a107cb5be319eed781c0 (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
// -*- c-basic-offset: 4 -*-
// -*- c-file-style:  "bsd" -*-

// This does some rather shoddy tests on a small selection of core classes.

#include "Lock.h"
#include "Composition.h"
#include "Segment.h"
#include "Event.h"

#include <cstdio>
#include <sys/times.h>
#include <iostream>

#include <pthread.h>
#include <unistd.h>

using namespace std;
using namespace Rosegarden;

static void*
writer_thread1(void *arg)
{
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    cout << "write_thread1 - init" << endl;

    Rosegarden::Composition *comp = 
        static_cast<Rosegarden::Composition*>(arg);

    Rosegarden::Composition::segmentcontainer segs = comp->getSegments();
    Rosegarden::Composition::segmentcontainer::iterator it = segs.begin();
    Rosegarden::Segment *segment = *it;

    Rosegarden::timeT insertTime = 50000;
    while (true)
    {
        usleep(90000);
        cout << "LENGTH = " << comp->getNbBars() << endl;
        segment->insert(new Event(Note::EventType, insertTime));
        insertTime += 96;
    }
}

static void*
write_thread2(void *arg)
{
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    cout << "write_thread2 - init" << endl;

    Rosegarden::Composition *comp = 
        static_cast<Rosegarden::Composition*>(arg);

    Rosegarden::Composition::segmentcontainer segs = comp->getSegments();
    Rosegarden::Composition::segmentcontainer::iterator it = segs.begin();
    Rosegarden::Segment *segment = *it;

    Rosegarden::timeT insertTime = 0;
    while (true)
    {
        usleep(50);
        cout << "LENGTH = " << comp->getNbBars() << endl;
        segment->insert(new Event(Note::EventType, insertTime));
        insertTime += 96;
    }
}


int
main(int argc, char **argv)
{
    clock_t st, et;
    struct tms spare;

    cout << "Threading test" << endl;

    pthread_t thread1;
    pthread_t thread2;
    Rosegarden::Composition comp;
    Rosegarden::Segment segment;
    comp.addSegment(&segment);

    if (pthread_create(&thread1, 0, writer_thread1, &comp))
    {
        cerr << "Couldn't start thread 1" << endl;
        exit(1);
    }
    pthread_detach(thread1);

    if (pthread_create(&thread2, 0, write_thread2, &comp))
    {
        cerr << "Couldn't start thread 2" << endl;
        exit(1);
    }
    pthread_detach(thread2);

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    static Lock lock;

    if (lock.getWriteLock(1))
    {
        cout << "got write lock" << endl;
    }

    if (lock.getWriteLock(0))
    {
        cout << "got second write lock" << endl;
    }
    else
    {
        cout << "couldn't get second write lock" << endl;
    }

    Rosegarden::timeT insertTime = 0;
    while(true)
    {
        usleep(50000);

        cout << "Inserting Event at time " << insertTime << endl;
        segment.insert(new Event(Note::EventType, insertTime));
        insertTime += 96;
    }

};