- adaptepte usleep -> Glib::usleep

- update to glib-2.34 Glib::Threads*

(not tested yet, am on a Mac)
master
Steve Clement 2013-07-15 22:39:25 +02:00
parent dacbfa9b29
commit bccde4f726
5 changed files with 44 additions and 45 deletions

View File

@ -5,45 +5,45 @@ Buffer::Buffer( std::string _id )
id = _id; id = _id;
} }
Buffer::~Buffer() Buffer::~Buffer()
{ {
} }
void Buffer::set(frame_t data) void Buffer::set(frame_t data)
{ {
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
frame = data; frame = data;
} }
} }
void Buffer::set_id(std::string _id) void Buffer::set_id(std::string _id)
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
id = _id; id = _id;
} }
void Buffer::set_selected(bool _selected) void Buffer::set_selected(bool _selected)
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
selected = _selected; selected = _selected;
} }
frame_t Buffer::get() frame_t Buffer::get()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
return frame; return frame;
} }
std::string Buffer::get_id() std::string Buffer::get_id()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
return id; return id;
} }
bool Buffer::get_selected() bool Buffer::get_selected()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
return selected; return selected;
} }

View File

@ -16,22 +16,22 @@ class Buffer : public sigc::trackable
public: public:
Buffer(std::string _id); Buffer(std::string _id);
~Buffer(); ~Buffer();
void set(frame_t); void set(frame_t);
frame_t get(); frame_t get();
std::string get_id(); std::string get_id();
void set_id(std::string id); void set_id(std::string id);
bool get_selected(); bool get_selected();
void set_selected(bool); void set_selected(bool);
private: private:
std::string id; std::string id;
frame_t frame; frame_t frame;
bool selected; bool selected;
Glib::Mutex mutex_; Glib::Threads::Mutex mutex_;
}; };
#endif #endif

View File

@ -1,6 +1,6 @@
#include "Buffers.h" #include "Buffers.h"
Buffers::Buffers() Buffers::Buffers()
{ {
id = ""; id = "";
} }
@ -14,26 +14,26 @@ Buffers::Buffers(int _bufnum)
Buffers::~Buffers() Buffers::~Buffers()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
buffers.clear(); buffers.clear();
} }
Buffer* Buffers::get(int index) Buffer* Buffers::get(int index)
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
return buffers[index]; return buffers[index];
} }
void Buffers::add() void Buffers::add()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
id += "1"; id += "1";
buffers.push_back( new Buffer( id ) ); buffers.push_back( new Buffer( id ) );
} }
void Buffers::remove(std::string _id) void Buffers::remove(std::string _id)
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Threads::Mutex::Lock lock(mutex_);
int size = buffers.size(); int size = buffers.size();
for( int i = 0; i < size; i++ ) for( int i = 0; i < size; i++ )
{ {

View File

@ -5,7 +5,7 @@
vector of pointers to "Buffer" objects. It automates locking during vector of pointers to "Buffer" objects. It automates locking during
all operations and generates "ID" hashes for the buffers upon creation. */ all operations and generates "ID" hashes for the buffers upon creation. */
/* TODO: * create hashes during buffer creation /* TODO: * create hashes during buffer creation
* throw and handle exceptions */ * throw and handle exceptions */
#include <glibmm.h> #include <glibmm.h>
@ -24,21 +24,21 @@ class Buffers : public sigc::trackable
public: public:
Buffers(); Buffers();
Buffers(int); Buffers(int);
~Buffers(); ~Buffers();
void add(); void add();
void remove(std::string); void remove(std::string);
void set_selected_buffer(std::string); void set_selected_buffer(std::string);
std::string get_selected_buffer(); std::string get_selected_buffer();
Buffer* get(int); Buffer* get(int);
private: private:
vector<Buffer*> buffers; vector<Buffer*> buffers;
std::string id; std::string id;
Glib::Mutex mutex_; Glib::Threads::Mutex mutex_;
std::string selected_buffer; std::string selected_buffer;
}; };
#endif #endif

View File

@ -9,7 +9,6 @@
#include "Buffer.h" #include "Buffer.h"
#include "Buffers.h" #include "Buffers.h"
#define NUMBUFS 100
#define NUMTHREADS 20 #define NUMTHREADS 20
#define USECS 1000 #define USECS 1000
@ -22,14 +21,14 @@ Buffers* buffers;
void reader(void); void reader(void);
void writer(void); void writer(void);
int main(void) int main(void)
{ {
srand( time(NULL) ); srand( time(NULL) );
Glib::thread_init(); Glib::init();
// our main loop with support for signals and all that jazz // our main loop with support for signals and all that jazz
//Glib::RefPtr<Glib::MainLoop> Main = Glib::MainLoop::create(); //Glib::RefPtr<Glib::MainLoop> Main = Glib::MainLoop::create();
buffers = new Buffers(NUMBUFS); buffers = new Buffers(NUMBUFS);
vector<Glib::Thread*> readers; vector<Glib::Thread*> readers;
@ -37,12 +36,12 @@ int main(void)
for(int i = 0; i < NUMTHREADS; i++) for(int i = 0; i < NUMTHREADS; i++)
{ {
readers.push_back( Glib::Thread::create( sigc::ptr_fun( &reader), false ) ); readers.push_back( Glib::Threads::Thread::create( sigc::ptr_fun( &reader), false ) );
writers.push_back( Glib::Thread::create( sigc::ptr_fun( &writer), false ) ); writers.push_back( Glib::Threads::Thread::create( sigc::ptr_fun( &writer), false ) );
} }
//Main->run(); //Main->run();
int count = 0; int count = 0;
while(1) { while(1) {
count++; count++;
@ -56,28 +55,28 @@ int main(void)
cout << endl; cout << endl;
} }
cout << endl; cout << endl;
for(int i = 0; i < SEGWIDTH; i++) for(int i = 0; i < SEGWIDTH; i++)
cout << frame.segments[i].r; cout << frame.segments[i].r;
cout << endl << endl; cout << endl << endl;
} }
usleep( 10000 ); Glib::usleep( 10000 );
}//*/ }//*/
return 0; return 0;
} }
void reader(void) void reader(void)
{ {
bool quit = false; bool quit = false;
frame_t frame; frame_t frame;
int bufnum = 0; int bufnum = 0;
while( !quit ) while( !quit )
{ {
bufnum = rand()%NUMBUFS; bufnum = rand()%NUMBUFS;
frame = buffers->get(bufnum)->get(); frame = buffers->get(bufnum)->get();
// cout << "read " << bufnum << endl; // cout << "read " << bufnum << endl;
usleep( rand()%USECS ); Glib::usleep( rand()%USECS );
} }
} }
@ -85,7 +84,7 @@ void writer(void)
{ {
frame_t frame; frame_t frame;
bool quit = false; bool quit = false;
int bufnum = 0; int bufnum = 0;
while( !quit ) while( !quit )
{ {
bufnum = rand()%NUMBUFS; bufnum = rand()%NUMBUFS;
@ -96,16 +95,16 @@ void writer(void)
frame.windows[i][j] = rand()%255; frame.windows[i][j] = rand()%255;
} }
} }
for(int i = 0; i < 12; i++) for(int i = 0; i < 12; i++)
{ {
frame.segments[i].r = 33+rand()%90; frame.segments[i].r = 33+rand()%90;
frame.segments[i].g = 33+rand()%90; frame.segments[i].g = 33+rand()%90;
frame.segments[i].b = 33+rand()%90; frame.segments[i].b = 33+rand()%90;
} }
buffers->get(bufnum)->set( frame ); buffers->get(bufnum)->set( frame );
// cout << "wrote " << bufnum << endl; // cout << "wrote " << bufnum << endl;
usleep( rand()%USECS ); Glib::usleep( rand()%USECS );
} }
} }