Skip to content

Instantly share code, notes, and snippets.

@oza
Created May 28, 2010 12:56
Show Gist options
  • Save oza/417118 to your computer and use it in GitHub Desktop.
Save oza/417118 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "task.h"
#include "queue.h"
#include "worker.h"
// name space
void hoge()
{
std::cout << "hoge" << std::endl;
}
void huga()
{
std::cout << "huga" << std::endl;
}
/* test program */
int main(int argc, char** argv)
{
V1::Queue *q = new V1::Queue(3);
#if 0
__sync_fetch_and_add(&a,1);
__sync_fetch_and_sub(&a,1);
__sync_fetch_and_sub(&a,1);
std::cout << a << std::endl;
__sync_bool_compare_and_swap(&a,-1,100);
std::cout << a << std::endl;
#else
V1::Task t1((void*)hoge);
V1::Task t2((void*)huga);
q->add(t1);
q->add(t2);
q->show();
#endif
}
#include<iostream>
main()
{
int a = 0;
__sync_fetch_and_add(&a,1);
__sync_fetch_and_sub(&a,1);
__sync_fetch_and_sub(&a,1);
std::cout << a << std::endl;
__sync_bool_compare_and_swap(&a,-1,100);
std::cout << a << std::endl;
}
all:
make a.out
a.out: a.cc worker.cc
g++ a.cc worker.cc
./a.out
clean:
rm -rf *.o a.out
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <cstdlib>
#include "task.h"
namespace V1 {
class Queue {
public:
Queue(size_t);
~Queue();
bool add(class Task&);
//bool add(class Task*);
int get_fd();
void show();
private:
size_t num;
size_t index;
Task** tasks;
int fd;
};
inline Queue::Queue(size_t num)
{
this->index = 0;
this->num = num;
this->tasks = (Task**)::malloc(num);
}
inline Queue::~Queue()
{
::free(this->tasks);
}
inline bool Queue::add(class Task& t)
{
int i;
i = __sync_fetch_and_add(&this->index, 1) % num;
tasks[i] = &t;
return true;
}
#if 0
inline bool Queue::add(class Task* t)
{
return this->add(&(*t));
}
#endif
inline int Queue::get_fd(void)
{
return this->fd;
}
inline void Queue::show(void)
{
for(int i = 0; i < this->index; ++i ){
std::cout << i << std::endl;
tasks[i]->show();
}
}
} // V1
#endif //__QUEUE_H__
#ifndef __TASK_H__
#define __TASK_H__
#include <iostream>
namespace V1 {
class Task {
public:
Task(void* handler=NULL, void* args=NULL,void* cookie=NULL);
~Task(){};
void enregister(void* handler, void* args=NULL,
void* cookie=NULL);
void show();
private:
void* handler;
void* args;
void* cookie;
};
inline Task::Task(void* handler, void* args,
void* cookie)
{
this->enregister(handler, args, cookie);
}
inline void Task::enregister(void* handler, void* args,
void* cookie)
{
this->handler = handler;
this->args = args;
this->cookie = cookie;
}
inline void Task::show(void)
{
std::cout << "handler " << this->handler << std::endl;;
std::cout << "args " << this->args << std::endl;;
std::cout << "cookie " << this->cookie << std::endl;;
}
} // namespace V1
#endif
A test queue
#include "worker.h"
namespace V1{
Worker::Worker(Queue &queue)
{
int efd;
struct epoll_event ev;
int rfd;
this->queue = &queue;
efd = ::epoll_create(0);
if(this->efd < 0){
::perror("epoll_create at Worker::Worker");
::abort();
}
rfd = queue.get_fd();
ev.events = EPOLLIN;
if(::epoll_ctl(efd, EPOLL_CTL_ADD, rfd,
&ev) < 0){
::perror("epoll_create at Worker::Worker");
::abort();
}
}
void Worker::run()
{
/* main loop for main thread */
while(1){
//epoll_wait();
}
}
}
#ifndef __WORKER_H__
#define __WORKER_H__
#include "queue.h"
#include <sys/epoll.h>
namespace V1{
class Worker {
public:
Worker(Queue &queue);
~Worker(){};
void run();
private:
Queue *queue;
int efd;
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment