mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-03 06:35:08 -04:00
Created V0.3.x branch and moved the head into the trunk directory.
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@246 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
commit
935745a08e
1318 changed files with 348809 additions and 0 deletions
27
libretroshare/src/util/Makefile
Normal file
27
libretroshare/src/util/Makefile
Normal file
|
@ -0,0 +1,27 @@
|
|||
|
||||
RS_TOP_DIR = ..
|
||||
include ../make.opt
|
||||
|
||||
OBJ = rsthreads.o rsdir.o
|
||||
|
||||
all : $(OBJ) librs dirtest dir2test
|
||||
|
||||
dirtest: $(OBJ) dirtest.o
|
||||
$(CC) $(CFLAGS) -o dirtest $(OBJ) dirtest.o $(RSLIBS)
|
||||
|
||||
dir2test: $(OBJ) dir2test.o
|
||||
$(CC) $(CFLAGS) -o dir2test $(OBJ) dir2test.o $(RSLIBS)
|
||||
|
||||
librs: $(OBJ)
|
||||
$(AR) r $(LIBRS) $(OBJ)
|
||||
$(RANLIB) $(LIBRS)
|
||||
|
||||
.cc.o:
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
clean:
|
||||
-/bin/rm $(OBJ) dirtest.o dir2test.o
|
||||
|
||||
clobber: clean
|
||||
-/bin/rm dirtest dir2test
|
||||
|
79
libretroshare/src/util/dir2test.cc
Normal file
79
libretroshare/src/util/dir2test.cc
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* dirtest.cc
|
||||
*
|
||||
* RetroShare Test Program.
|
||||
*
|
||||
* Copyright 2004-2007 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "util/rsdir.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int processpath(std::string path);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
std::string path1 = "/home/tst1//test2///test3/";
|
||||
std::string path2 = "home2/tst4//test5///test6";
|
||||
std::string path3 = "//home3";
|
||||
std::string path4 = "//";
|
||||
std::string path5 = "/a/b/c/d/";
|
||||
std::string path6 = "a//b/c//d";
|
||||
|
||||
processpath(path1);
|
||||
processpath(path2);
|
||||
processpath(path3);
|
||||
processpath(path4);
|
||||
processpath(path5);
|
||||
processpath(path6);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int processpath(std::string path)
|
||||
{
|
||||
std::string pathtogo = path;
|
||||
while(pathtogo != "")
|
||||
{
|
||||
std::string basedir = RsDirUtil::getRootDir(pathtogo);
|
||||
std::string rempath = RsDirUtil::removeRootDir(pathtogo);
|
||||
std::string topdir = RsDirUtil::getTopDir(pathtogo);
|
||||
std::string remtoppath = RsDirUtil::removeTopDir(pathtogo);
|
||||
|
||||
std::cerr << "Processing: \"" << pathtogo << "\"" << std::endl;
|
||||
std::cerr << "\tRootDir : \"" << basedir << "\"" << std::endl;
|
||||
std::cerr << "\tRemaining: \"" << rempath << "\"" << std::endl;
|
||||
std::cerr << "\tTopDir : \"" << topdir << "\"" << std::endl;
|
||||
std::cerr << "\tRemaining(Top): \"" << remtoppath << "\"" << std::endl;
|
||||
std::cerr << std::endl;
|
||||
|
||||
pathtogo = rempath;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
99
libretroshare/src/util/dirtest.cc
Normal file
99
libretroshare/src/util/dirtest.cc
Normal file
|
@ -0,0 +1,99 @@
|
|||
|
||||
/*
|
||||
* "$Id: dirtest.cc,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
|
||||
*
|
||||
* RetroShare C++ Interface.
|
||||
*
|
||||
* Copyright 2004-2007 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "util/rsdir.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
bool testRsDirUtils(std::string path);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
std::list<std::string> dirs;
|
||||
std::list<std::string>::iterator it;
|
||||
dirs.push_back("/incoming/htuyr/file.txt");
|
||||
dirs.push_back("/incoming/htuyr/file.txt ");
|
||||
dirs.push_back("/incoming/htuyr/file.txt/");
|
||||
dirs.push_back("/incoming/htuyr/file.txt//");
|
||||
dirs.push_back("/incoming/htuyr//file.txt//");
|
||||
dirs.push_back("/incoming/htuyr//file .txt");
|
||||
dirs.push_back("/incoming/htuyr/Q");
|
||||
dirs.push_back("/incoming/htuyr/Q//");
|
||||
dirs.push_back("/incoming/htuyr/Q/");
|
||||
dirs.push_back("/incoming/htuyr/Q/text");
|
||||
dirs.push_back("/home/tst1//test2///test3/");
|
||||
dirs.push_back("home2/tst4//test5///test6");
|
||||
dirs.push_back("//home3");
|
||||
dirs.push_back("//");
|
||||
dirs.push_back("A");
|
||||
dirs.push_back("ABC");
|
||||
dirs.push_back("////ABC////");
|
||||
dirs.push_back("A/B/C");
|
||||
|
||||
for(it = dirs.begin(); it != dirs.end(); it++)
|
||||
{
|
||||
testRsDirUtils(*it);
|
||||
}
|
||||
}
|
||||
|
||||
bool testRsDirUtils(std::string path)
|
||||
{
|
||||
|
||||
std::cerr << "RsUtilTest input: [" << path << "]";
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::string top = RsDirUtil::getTopDir(path);
|
||||
std::string root = RsDirUtil::getRootDir(path);
|
||||
std::string topdirs = RsDirUtil::removeRootDir(path);
|
||||
std::string topdirs2 = RsDirUtil::removeRootDirs(path, root);
|
||||
std::string restdirs = RsDirUtil::removeTopDir(path);
|
||||
std::list<std::string> split;
|
||||
std::list<std::string>::iterator it;
|
||||
RsDirUtil::breakupDirList(path, split);
|
||||
|
||||
std::cerr << "\tTop: [" << top << "]";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\tRest: [" << restdirs << "]";
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "\tRoot: [" << root << "]";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\tRemoveRoot: [" << topdirs << "]";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\tSplit Up ";
|
||||
for(it = split.begin(); it != split.end(); it++)
|
||||
{
|
||||
std::cerr << ":" << (*it);
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
std::cerr << std::endl;
|
||||
return true;
|
||||
}
|
279
libretroshare/src/util/rsdir.cc
Normal file
279
libretroshare/src/util/rsdir.cc
Normal file
|
@ -0,0 +1,279 @@
|
|||
|
||||
/*
|
||||
* "$Id: rsdir.cc,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
|
||||
*
|
||||
* RetroShare C++ Interface.
|
||||
*
|
||||
* Copyright 2004-2007 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
// Includes for directory creation.
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util/rsdir.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
std::string RsDirUtil::getTopDir(std::string dir)
|
||||
{
|
||||
std::string top;
|
||||
|
||||
/* find the subdir: [/][dir1.../]<top>[/]
|
||||
*/
|
||||
int i,j;
|
||||
int len = dir.length();
|
||||
for(j = len - 1; (j > 0) && (dir[j] == '/'); j--);
|
||||
for(i = j; (i > 0) && (dir[i] != '/'); i--);
|
||||
|
||||
if (dir[i] == '/')
|
||||
i++;
|
||||
|
||||
for(; i <= j; i++)
|
||||
{
|
||||
top += dir[i];
|
||||
}
|
||||
|
||||
return top;
|
||||
}
|
||||
|
||||
std::string RsDirUtil::removeTopDir(std::string dir)
|
||||
{
|
||||
std::string rest;
|
||||
|
||||
/* remove the subdir: [/][dir1.../]<top>[/]
|
||||
*/
|
||||
int i,j;
|
||||
int len = dir.length();
|
||||
for(j = len - 1; (j > 0) && (dir[j] == '/'); j--);
|
||||
for(i = j; (i >= 0) && (dir[i] != '/'); i--);
|
||||
|
||||
/* remove any more slashes */
|
||||
for(; (i >= 0) && (dir[i] == '/'); i--);
|
||||
|
||||
for(j = 0; j <= i; j++)
|
||||
{
|
||||
rest += dir[j];
|
||||
}
|
||||
|
||||
return rest;
|
||||
}
|
||||
|
||||
std::string RsDirUtil::getRootDir(std::string dir)
|
||||
{
|
||||
std::string root;
|
||||
|
||||
/* find the subdir: [/]root[/...]
|
||||
*/
|
||||
int i,j;
|
||||
int len = dir.length();
|
||||
for(i = 0; (i < len) && (dir[i] == '/'); i++);
|
||||
for(j = i; (j < len) && (dir[j] != '/'); j++);
|
||||
if (i == j)
|
||||
return root; /* empty */
|
||||
for(; i < j; i++)
|
||||
{
|
||||
root += dir[i];
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
std::string RsDirUtil::removeRootDir(std::string path)
|
||||
{
|
||||
unsigned int i, j;
|
||||
unsigned int len = path.length();
|
||||
std::string output;
|
||||
|
||||
/* chew leading '/'s */
|
||||
for(i = 0; (i < len) && (path[i] == '/'); i++);
|
||||
if (i == len)
|
||||
return output; /* empty string */
|
||||
|
||||
for(j = i; (j < len) && (path[j] != '/'); j++); /* run to next '/' */
|
||||
for(; (j < len) && (path[j] == '/'); j++); /* chew leading '/'s */
|
||||
|
||||
for(; j < len; j++)
|
||||
{
|
||||
output += path[j];
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string RsDirUtil::removeRootDirs(std::string path, std::string root)
|
||||
{
|
||||
/* too tired */
|
||||
std::string notroot;
|
||||
//std::cerr << "remoteRootDir( TODO! )";
|
||||
|
||||
unsigned int i = 0, j = 0;
|
||||
|
||||
/* catch empty data */
|
||||
if ((root.length() < 1) || (path.length() < 1))
|
||||
return notroot;
|
||||
|
||||
if ((path[0] == '/') && (root[0] != '/'))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
for(; (i < path.length()) && (j < root.length()) && (path[i] == root[j]); i++, j++);
|
||||
|
||||
/* should have consumed root. */
|
||||
if (j == root.length())
|
||||
{
|
||||
//std::cerr << "matched root!" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
//std::cerr << "failed i: " << i << ", j: " << j << std::endl;
|
||||
//std::cerr << "root: " << root << " path: " << path << std::endl;
|
||||
return notroot;
|
||||
}
|
||||
|
||||
if (path[i] == '/')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
for(; i < path.length(); i++)
|
||||
{
|
||||
notroot += path[i];
|
||||
}
|
||||
|
||||
//std::cerr << "Found NotRoot: " << notroot << std::endl;
|
||||
|
||||
return notroot;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int RsDirUtil::breakupDirList(std::string path,
|
||||
std::list<std::string> &subdirs)
|
||||
{
|
||||
int start = 0;
|
||||
unsigned int i;
|
||||
for(i = 0; i < path.length(); i++)
|
||||
{
|
||||
if (path[i] == '/')
|
||||
{
|
||||
if (i - start > 0)
|
||||
{
|
||||
subdirs.push_back(path.substr(start, i-start));
|
||||
}
|
||||
start = i+1;
|
||||
}
|
||||
}
|
||||
// get the final one.
|
||||
if (i - start > 0)
|
||||
{
|
||||
subdirs.push_back(path.substr(start, i-start));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool RsDirUtil::checkCreateDirectory(std::string dir)
|
||||
{
|
||||
struct stat buf;
|
||||
int val = stat(dir.c_str(), &buf);
|
||||
if (val == -1)
|
||||
{
|
||||
// directory don't exist. create.
|
||||
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#ifndef WINDOWS_SYS // UNIX
|
||||
if (-1 == mkdir(dir.c_str(), 0777))
|
||||
#else // WIN
|
||||
if (-1 == mkdir(dir.c_str()))
|
||||
#endif
|
||||
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
||||
{
|
||||
std::cerr << "check_create_directory() Fatal Error --";
|
||||
std::cerr <<std::endl<< "\tcannot create:" <<dir<<std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cerr << "check_create_directory()";
|
||||
std::cerr <<std::endl<< "\tcreated:" <<dir<<std::endl;
|
||||
}
|
||||
else if (!S_ISDIR(buf.st_mode))
|
||||
{
|
||||
// Some other type - error.
|
||||
std::cerr<<"check_create_directory() Fatal Error --";
|
||||
std::cerr<<std::endl<<"\t"<<dir<<" is nor Directory"<<std::endl;
|
||||
return 0;
|
||||
}
|
||||
std::cerr << "check_create_directory()";
|
||||
std::cerr <<std::endl<< "\tDir Exists:" <<dir<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include <dirent.h>
|
||||
//#include <sys/types.h>
|
||||
//#include <sys/stat.h>
|
||||
//#include <fcntl.h>
|
||||
//#include <unistd.h>
|
||||
|
||||
bool RsDirUtil::cleanupDirectory(std::string cleandir, std::list<std::string> keepFiles)
|
||||
{
|
||||
|
||||
/* check for the dir existance */
|
||||
DIR *dir = opendir(cleandir.c_str());
|
||||
std::list<std::string>::const_iterator it;
|
||||
|
||||
if (!dir)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
struct dirent *dent;
|
||||
struct stat buf;
|
||||
|
||||
while(NULL != (dent = readdir(dir)))
|
||||
{
|
||||
/* check entry type */
|
||||
std::string fname = dent -> d_name;
|
||||
std::string fullname = cleandir + "/" + fname;
|
||||
|
||||
if (-1 != stat(fullname.c_str(), &buf))
|
||||
{
|
||||
/* only worry about files */
|
||||
if (S_ISREG(buf.st_mode))
|
||||
{
|
||||
/* check if we should keep it */
|
||||
if (keepFiles.end() == (it = std::find(keepFiles.begin(), keepFiles.end(), fname)))
|
||||
{
|
||||
/* can remove */
|
||||
remove(fullname.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* close directory */
|
||||
closedir(dir);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
52
libretroshare/src/util/rsdir.h
Normal file
52
libretroshare/src/util/rsdir.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
|
||||
/*
|
||||
* "$Id: rsdir.h,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
|
||||
*
|
||||
* RetroShare C++ Interface.
|
||||
*
|
||||
* Copyright 2004-2007 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef RSUTIL_DIRFNS_H
|
||||
#define RSUTIL_DIRFNS_H
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
namespace RsDirUtil {
|
||||
|
||||
std::string getTopDir(std::string);
|
||||
std::string getRootDir(std::string);
|
||||
std::string removeRootDir(std::string path);
|
||||
std::string removeTopDir(std::string dir);
|
||||
|
||||
std::string removeRootDirs(std::string path, std::string root);
|
||||
|
||||
int breakupDirList(std::string path,
|
||||
std::list<std::string> &subdirs);
|
||||
|
||||
bool checkCreateDirectory(std::string dir);
|
||||
bool cleanupDirectory(std::string dir, std::list<std::string> keepFiles);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
59
libretroshare/src/util/rsthreads.cc
Normal file
59
libretroshare/src/util/rsthreads.cc
Normal file
|
@ -0,0 +1,59 @@
|
|||
|
||||
/*
|
||||
* "$Id: rsthreads.cc,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
|
||||
*
|
||||
* RetroShare C++ Interface.
|
||||
*
|
||||
* Copyright 2004-2007 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "rsthreads.h"
|
||||
|
||||
extern "C" void* rsthread_init(void* p)
|
||||
{
|
||||
RsThread *thread = (RsThread *) p;
|
||||
if (!thread)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
thread -> run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
pthread_t createThread(RsThread &thread)
|
||||
{
|
||||
pthread_t tid;
|
||||
void *data = (void *) (&thread);
|
||||
|
||||
thread.mMutex.lock();
|
||||
{
|
||||
pthread_create(&tid, 0, &rsthread_init, data);
|
||||
thread.mTid = tid;
|
||||
}
|
||||
thread.mMutex.unlock();
|
||||
|
||||
return tid;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
67
libretroshare/src/util/rsthreads.h
Normal file
67
libretroshare/src/util/rsthreads.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
#ifndef RSIFACE_THREADS_H
|
||||
#define RSIFACE_THREADS_H
|
||||
|
||||
/*
|
||||
* "$Id: rsthreads.h,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
|
||||
*
|
||||
* RetroShare C++ Interface.
|
||||
*
|
||||
* Copyright 2004-2006 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
/* RsIface Thread Wrappers */
|
||||
|
||||
class RsMutex
|
||||
{
|
||||
public:
|
||||
|
||||
RsMutex() { pthread_mutex_init(&realMutex, NULL); }
|
||||
~RsMutex() { pthread_mutex_destroy(&realMutex); }
|
||||
void lock() { pthread_mutex_lock(&realMutex); }
|
||||
void unlock() { pthread_mutex_unlock(&realMutex); }
|
||||
bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); }
|
||||
|
||||
private:
|
||||
pthread_mutex_t realMutex;
|
||||
};
|
||||
|
||||
class RsThread;
|
||||
|
||||
/* to create a thread! */
|
||||
pthread_t createThread(RsThread &thread);
|
||||
|
||||
class RsThread
|
||||
{
|
||||
public:
|
||||
RsThread() { return; }
|
||||
virtual ~RsThread() { return; }
|
||||
|
||||
virtual void start() { createThread(*this); }
|
||||
virtual void run() = 0; /* called once the thread is started */
|
||||
|
||||
pthread_t mTid;
|
||||
RsMutex mMutex;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue