📄 vscp_os.c
字号:
#include "vscp_os.h"
#ifdef WIN32
void CriticalSectionInit(CriticalSection * cs)
{
InitializeCriticalSection(cs);
}
void CriticalSectionEnter(CriticalSection * cs)
{
EnterCriticalSection(cs);
}
void CriticalSectionLeave(CriticalSection * cs)
{
LeaveCriticalSection(cs);
}
void CriticalSectionDelete(CriticalSection * cs)
{
DeleteCriticalSection(cs);
}
Semaphore SemaphoreCreate(Semaphore * sem,void * attrib,\
int initnum,int maxnum,char * name)
{
sem; /* not use */
return CreateSemaphore(attrib,initnum,maxnum,name);
}
int SemaphoreRelease(Semaphore * sem,int addnum,void * oldnumptr)
{
if(ReleaseSemaphore(*sem,addnum,oldnumptr) == TRUE)
return 0;
else
return 1;
}
Event EventCreate(Event * event,void * attrib,int init,int manual, unsigned short * name)
{
event; /* not use */
return CreateEvent(attrib,init,manual,name);
}
int EventSet(Event * event)
{
if(SetEvent(*event) == TRUE)
return 0;
else
return 1;
}
int SingleObjectWaitFor(Semaphore * sem,unsigned long milliseconds)
{
return WaitForSingleObject(*sem,milliseconds);
}
Handle sysThreadBegin(const char *origfile, int line,unsigned int stack_size, \
void * start_address, void * arglist,unsigned int initflag, \
void * threadid,int detachstate,int priority )
{
typedef unsigned ( __stdcall *START_ADDR )( void * );
priority; detachstate; /* not use */
return (Handle)_beginthreadex(NULL,stack_size, \
(START_ADDR)start_address, arglist,initflag,threadid);
}
Handle ThreadCreat(unsigned int stack_size, void * start_address,\
void * arglist,unsigned int initflag, unsigned int * threadid)
{
typedef unsigned long( __stdcall *START_ADDR )( void * );
return (Handle)CreateThread(NULL,stack_size, \
(START_ADDR)start_address, arglist,initflag, \
(LPDWORD)threadid);
}
int ThreadPrioritySet(Handle thread, int priority)
{
if(SetThreadPriority(thread,priority) == TRUE)
return 0;
else
return 1;
}
Handle ThreadGetCurrent(void)
{
return GetCurrentThread();
}
void sysThreadEnd(const char *origfile, int line, void * retval)
{
if(retval == NULL)
_endthreadex(0);
else
_endthreadex(*(unsigned int*)retval);
}
void ThreadCancel(Handle handle)
{
TerminateThread(handle,0);
HandleClose(handle);
}
void ProcessEnd(int retvalue)
{
exit(retvalue);
}
int HandleClose(Handle handle)
{
if(CloseHandle(handle) == TRUE)
return 0;
else
return 1;
}
void ThreadSleep(unsigned int milliseconds)
{
Sleep(milliseconds);
}
int GetSysTimeByMilliSecond(void)
{
return GetTickCount();
}
void GetTimeToMM(TimePTR ctime)
{
struct tm *newtime;
time_t ltime;
time(<ime);
newtime = localtime(<ime);
ctime->year = (unsigned short)(newtime->tm_year + 1900);
ctime->mon = (unsigned char)(newtime->tm_mon+1);
ctime->day = (unsigned char)newtime->tm_mday;
ctime->hour = (unsigned char)newtime->tm_hour;
ctime->min = (unsigned char)newtime->tm_min;
ctime->sec = (unsigned char)newtime->tm_sec;
ctime->msec = 0;
}
int RebootSystem(int mode)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
/* Get a token for this process. */
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return 1;
/* Get the LUID for the shutdown privilege. */
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; /* one privilege to set */
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
/* Get the shutdown privilege for this process. */
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
/* Cannot test the return value of AdjustTokenPrivileges. */
if (GetLastError() != ERROR_SUCCESS)
return 1;
/* Shut down the system and force all applications to close. */
if(mode == LOGOFF)
{
if(!ExitWindowsEx(EWX_LOGOFF | EWX_FORCE , 0))
return 1;
}
else
{
if(mode == REBOOT)
{
if(!ExitWindowsEx(EWX_REBOOT | EWX_FORCE , 0))
return 1;
}
else
{
return 1;
}
}
return 0;
}
int GetCurDir(void* envname,unsigned int varlen)
{
unsigned int errorno;
errorno = GetCurrentDirectory(varlen,(LPTSTR)envname);
if(errorno > 0 || errorno <= varlen)
return 0;
else
return 1;
}
char* sysitoa(int num,char* str,int radix)
{
return itoa(num,str,radix);
}
FILE * __sysfopen(const char *origfile, int line,const char *filename, const char *mode)
{
return fopen(filename,mode);
}
#define TIMES_OVERFLOW (0xffffffff)
static unsigned int tick_100ms = 0;
int tick10msThread(void *para)
{
unsigned int lasttick, nowtick;
unsigned int ofcount = 0;
for (;;)
{
nowtick = GetTickCount();
if (nowtick < lasttick)
ofcount ++;
tick_100ms = (ofcount * (TIMES_OVERFLOW / 100)) + (nowtick / 100);
lasttick = nowtick;
ThreadSleep(10);
}
return (0);
}
unsigned int GetSysTimeBy100ms(void)
{
return (tick_100ms);
}
int __sysfclose(const char *origfile, int line, FILE *fp)
{
// char buf[256];
// char ofname[256];
char *fn = NULL;
if (NULL == fp) {
return (-1);
}
/* FileNameTruncPath(origfile, &fn);
GetOpenFileName(fileno(fp), ofname, sizeof(ofname));
sprintf(buf, "%22s:%8d is %7s by %15s-%04d@%u.",
ofname, fileno(fp), "closed", fn, line, (unsigned int)getpid() );
buf[sizeof(buf) - 1] = '\0';
DelOpenFileRecord(fileno(fp));
if (fclose(fp) == -1)
return (-1);
WriteLog(buf);*/
return (0);
}
#endif
#ifdef LINUX
void CriticalSectionInit( CriticalSection * cs)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(cs,&attr);
}
void CriticalSectionEnter( CriticalSection * cs)
{
pthread_mutex_lock(cs);
}
void CriticalSectionLeave( CriticalSection * cs)
{
pthread_mutex_unlock(cs);
}
void CriticalSectionDelete( CriticalSection * cs)
{
pthread_mutex_destroy(cs);
}
Semaphore SemaphoreCreate(Semaphore * sem,void * attrib,\
int initnum,int maxnum,char * name)
{
if(sem_init(sem,0,initnum) != 0)
printf("Semaphore initialization failed");
return *sem;
}
int SemaphoreRelease(Semaphore * sem,int addnum,void * oldnumptr)
{
int i;
for(i=0;i<addnum;i++)
sem_post(sem);
return 0;
}
Event EventCreate(Event * event,void * attrib,unsigned int init,unsigned int manual, char * name)
{
if(sem_init(event,0,init) != 0)
printf("Event initialization failed");
return *event;
}
int EventSet(Event * event)
{
return sem_post(event);
}
int SingleObjectWaitFor(Semaphore * sem,unsigned int milliseconds)
{
int ret_wait = -1;
struct timeval now;
long lTimeSet, lTimeNow;
switch(milliseconds)
{
case 0:
if(sem_trywait(sem) == -1)
return WAIT_TIMEOUT;
else
return WAIT_OBJECT_0;
case INFINITE:
sem_wait(sem);
return WAIT_OBJECT_0;
default:
gettimeofday(&now, 0);
lTimeNow = now.tv_sec * 1000000 + now.tv_usec;
lTimeSet = lTimeNow + milliseconds * 1000;
while (ret_wait == -1 && lTimeNow < lTimeSet)
{
ret_wait = sem_trywait(sem);
gettimeofday(&now, 0);
lTimeNow = now.tv_sec * 1000000 + now.tv_usec;
}
if (ret_wait == 0)
return WAIT_OBJECT_0;
else
return WAIT_TIMEOUT;
}
}
int SemaphorePost(Semaphore * sem)
{
return sem_post(sem);
}
/*2004-06-17 system moni*/
Handle sysThreadBegin(const char *origfile, int line, const char *target_name, \
unsigned int stack_size,void * start_address, void * arglist, \
unsigned int initflag,void * threadaddr, int detachstate, \
int priority)
{
Handle thread;
pthread_attr_t thread_attr;
struct sched_param param;
if(pthread_attr_init(&thread_attr) != 0)
return (Handle)NULL;
if(pthread_attr_setdetachstate(&thread_attr,detachstate) != 0)
return (Handle)NULL;
if(priority == 0)
{
param.sched_priority = priority;
if(pthread_attr_setschedparam(&thread_attr,¶m) != 0)
return (Handle)NULL;
}
/*
if(pthread_attr_setstacksize(&thread_attr,stack_size) != 0)
return (Handle)NULL;
*/
if(pthread_create(&thread,&thread_attr,start_address,arglist) != 0)
{
return (Handle)NULL;
}
else
{
THREAD_RECORD_T thd;
char buf[256];
memset(&thd, 0, sizeof(THREAD_RECORD_T));
thd.th_time = time(NULL);
thd.th_origname = origfile;
thd.th_line = line;
thd.th_id = thread;
thd.th_pid = 0xff;
thd.th_targetname = target_name;
AddThreadRecord(&thd);
snprintf(buf, sizeof(buf), "%20s():%8u is %7s by %15s-%04d@%u.",
thd.th_targetname, (unsigned int)thd.th_id, "created", thd.th_origname, thd.th_line, (unsigned int)getpid() );
buf[sizeof(buf) - 1] = '\0';
sendLogMess(LOG_SYSRES_ONLY, buf, sizeof(buf));
return thread;
}
}
int ThreadPrioritySet(Handle thread, int priority)
{
return 0;
}
Handle ThreadGetCurrent(void)
{
return pthread_self();
}
void sysThreadEnd(const char *origfile, int line, void * retval)
{
char buf[256];
char thname[50];
pthread_t thid;
thid = pthread_self();
GetThreadName(thid, thname, sizeof(thname));
snprintf(buf, sizeof(buf), "%20s():%8d is %7s by %15s-%04d@%u.",
thname, (unsigned int)thid, "deleted", origfile, line, (unsigned int)getpid() );
buf[sizeof(buf) - 1] = '\0';
DelThreadRecord(thid);
sendLogMess(LOG_SYSRES_ONLY, buf, sizeof(buf));
pthread_exit(retval);
}
void ThreadCancel(Handle handle)
{
pthread_cancel(handle);
}
void ProcessEnd(int retvalue)
{
DumpAllLogMess();
exit(retvalue);
}
int HandleClose(Handle handle)
{
return 0;
}
void ThreadSleep(unsigned int milliseconds)
{
struct timeval tv;
tv.tv_sec = milliseconds/1000;
tv.tv_usec = (milliseconds*1000)%1000000;
select(0,0,0,0,&tv);
/* usleep(milliseconds*1000);*/
}
int GetCurDir(void* envname,unsigned int varlen)
{
char *errorno;
errorno = getcwd(envname,varlen);
if(errorno != NULL)
return 0;
else
return 1;
}
int GetSysTimeByMilliSecond(void)
{
struct tms buf;
clock_t tickcount;
tickcount = (times(&buf) * 10);
return tickcount;
}
void GetTimeToMM(TimePTR ctime)
{
struct tm *newtime;
time_t ltime;
time(<ime);
newtime = localtime(<ime);
ctime->year = (unsigned short)(newtime->tm_year + 1900);
ctime->mon = (unsigned char)(newtime->tm_mon+1);
ctime->day = (unsigned char)newtime->tm_mday;
ctime->hour = (unsigned char)newtime->tm_hour;
ctime->min = (unsigned char)newtime->tm_min;
ctime->sec = (unsigned char)newtime->tm_sec;
ctime->msec = 0;
}
char* sysstrupr(char* str)
{
char * strptr;
int i;
strptr = str;
for(i=0;i<strlen(str);i++,strptr++)
{
if(*strptr >= 'a' || *strptr <= 'z')
*strptr = toupper(*strptr);
}
return str;
}
char* sysitoa(int num,char* str,int radix)
{
char strbuf[32],i;
int strlen;
strlen = 0;
while(num != 0&&strlen < 32)
{
if((num % radix) < 10)
strbuf[strlen] = (num % radix) + 48;
else
strbuf[strlen] = (num % radix) + 87;
num /= radix;
strlen++;
}
for(i=0;i<strlen;i++)
{
*(str+i) = strbuf[strlen-i-1];
}
*(str+i) = '\0';
return str;
}
/*2004-06-17 system moni*/
static int FileNameTruncPath(const char *ifn, char **ofn)
{
char *ptr = NULL;
if (NULL == ifn || NULL == ofn) {
return (-1);
}
ptr = strrchr(ifn, '/');
if (NULL == ptr) {
ptr = ifn;
} else {
ptr++;
}
*ofn = ptr;
return (0);
}
FILE * __sysfopen(const char *origfile, int line, const char *targetfile, const char *mode)
{
FILE *fp = NULL;
OPENFILE_RECORD_T ofd;
char buf[256];
char *fn = NULL;
if (NULL == targetfile) {
return (NULL);
}
if ((fp = fopen(targetfile, mode)) == NULL)
return (NULL);
ofd.of_time = time(NULL);
ofd.of_origname = origfile;
ofd.of_line = line;
ofd.of_desc = fileno(fp);
FileNameTruncPath(targetfile, &fn);
strncpy(ofd.of_targetname, fn, sizeof(ofd.of_targetname));
AddOpenFileRecord(&ofd);
FileNameTruncPath(ofd.of_origname, &fn);
snprintf(buf, sizeof(buf), "%22s:%8d is %7s by %15s-%04d@%u.",
ofd.of_targetname, ofd.of_desc, "opened", fn, ofd.of_line, (unsigned int)getpid() );
buf[sizeof(buf) - 1] = '\0';
sendLogMess(LOG_SYSRES_ONLY, buf, sizeof(buf));
return fp;
}
int __sysfclose(const char *origfile, int line, FILE *fp)
{
char buf[256];
char ofname[256];
char *fn = NULL;
if (NULL == fp) {
return (-1);
}
FileNameTruncPath(origfile, &fn);
GetOpenFileName(fileno(fp), ofname, sizeof(ofname));
snprintf(buf, sizeof(buf), "%22s:%8d is %7s by %15s-%04d@%u.",
ofname, fileno(fp), "closed", fn, line, (unsigned int)getpid() );
buf[sizeof(buf) - 1] = '\0';
DelOpenFileRecord(fileno(fp));
if (fclose(fp) == -1)
return (-1);
sendLogMess(LOG_SYSRES_ONLY, buf, sizeof(buf));
return (0);
}
#define TIMES_OVERFLOW (0xffffffff)
static unsigned int tick_100ms = 0;
int tick10msThread(void *para)
{
struct tms buf;
unsigned int lasttick, nowtick;
unsigned int ofcount = 0;
#ifdef LINUX
SetThreadPID(ThreadGetCurrent(), getpid(), 10);
#endif
for (;;)
{
nowtick = times(&buf);
if (nowtick < lasttick)
ofcount ++;
tick_100ms = (ofcount * (TIMES_OVERFLOW / 10)) + (nowtick / 10);
lasttick = nowtick;
ThreadSleep(10);
}
return (0);
}
unsigned int GetSysTimeBy100ms(void)
{
return (tick_100ms);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -