Initial checkin of unified hierarchy of WPILib 2015

This commit is contained in:
Brad Miller
2013-12-15 18:30:16 -05:00
commit 3178911eef
1560 changed files with 410007 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "stdafx.h"
#include "OSAL/Synchronized.h"
//TODO see what the STATUS is suppose to return for success
STATUS semGive (SEM_ID semId)
{
::LeaveCriticalSection( semId );
return 0;
}
STATUS semTake (SEM_ID semId, int timeout)
{
if (timeout==WAIT_FOREVER)
::EnterCriticalSection( semId );
else
{
BOOL result;
int TimeOut=0;
do
{
result=::TryEnterCriticalSection( semId );
if (result==0)
Sleep(10);
} while ((result==0)&&(TimeOut++<timeout));
assert(result!=0); //TODO timeout
}
return 0;
}
SEM_ID semMCreate (int options)
{
SEM_ID ret=new CRITICAL_SECTION;
::InitializeCriticalSection( ret );
return ret;
}
STATUS semDelete (SEM_ID semId)
{
::DeleteCriticalSection( semId );
delete semId;
return 0;
}
/**
* Synchronized class deals with critical regions.
* Declare a Synchronized object at the beginning of a block. That will take the semaphore.
* When the code exits from the block it will call the destructor which will give the semaphore.
* This ensures that no matter how the block is exited, the semaphore will always be released.
* Use the CRITICAL_REGION(SEM_ID) and END_REGION macros to make the code look cleaner (see header file)
* @param semaphore The semaphore controlling this critical region.
*/
NTSynchronized::NTSynchronized(SEM_ID semaphore)
{
usingSem = false;
m_semaphore = semaphore;
semTake(m_semaphore, WAIT_FOREVER);
}
NTSynchronized::NTSynchronized(NTReentrantSemaphore& semaphore)
{
usingSem = true;
m_sem = &semaphore;
m_sem->take();
}
/**
* This destructor unlocks the semaphore.
*/
NTSynchronized::~NTSynchronized()
{
if(usingSem)
m_sem->give();
else
semGive(m_semaphore);
}

View File

@@ -0,0 +1,33 @@
/*
* System.cpp
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#include "windows.h"
#include "networktables2/util/System.h"
//#include "semLib.h"
#include <stdio.h>
//#include <sysLib.h> // for sysClkRateGet
//#include <usrLib.h> // for taskDelay
//timeGetTime() uses Winmm.lib
#pragma comment (lib,"Winmm.lib")
#pragma comment( lib, "Ws2_32" )
void sleep_ms(unsigned long ms){
//taskDelay((INT32)((double)sysClkRateGet() * ms / 1000));
Sleep(ms);
}
unsigned long currentTimeMillis(){
//struct timespec tp;
//clock_gettime(CLOCK_REALTIME,&tp);
DWORD dwTick_ = timeGetTime();
//return tp.tv_sec*1000 + tp.tv_nsec/1000;
return (long)dwTick_;
}
void writeWarning(const char* message){
fprintf(stderr, "%s\n", message);
fflush(stderr);
//TODO implement write warning with wpilib error stuff
}

View File

@@ -0,0 +1,315 @@
#include "stdafx.h"
#include "OSAL/Task.h"
#include <string.h>
#include <Windows.h>
//const UINT32 NTTask::kDefaultPriority;
//const INT32 NTTask::kInvalidTaskID;
/**
* Create but don't launch a task.
* @param name The name of the task. "FRC_" will be prepended to the task name.
* @param function The address of the function to run as the new task.
* @param priority The priority for the task.
* @param stackSize The size of the stack for the task
*/
NTTask::NTTask(const char* name, FUNCPTR function, INT32 priority, UINT32 stackSize)
{
//m_taskID = kInvalidTaskID;
m_Handle=NULL;
m_function = function;
m_priority = priority;
m_stackSize = stackSize;
m_taskName = new char[strlen(name) + 5];
strcpy(m_taskName, "FRC_");
strcpy(m_taskName+4, name);
//TODO see if we want to debug out this... it may be interesting info
#if 0
static INT32 instances = 0;
instances++;
#endif
}
NTTask::~NTTask()
{
//if (m_taskID != kInvalidTaskID) Stop();
if (m_Handle)
Stop();
delete [] m_taskName;
m_taskName = NULL;
}
// The thread callback
DWORD thread_proc( void *p_ptr )
{ // Get the pointer to myself
NTTask *p_this = (NTTask*)p_ptr;
assert( p_this );
(*p_this->m_function)( p_this->m_Arg );
return 0;
}
//This sets the name of the thread, which can help to identify threads in win32
#define MS_VC_EXCEPTION 0x406D1388
static void set_thread_name( const char *p_thread_name, DWORD ID )
{
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{ DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
// Set the information
THREADNAME_INFO info = { 0x1000, p_thread_name, ID, 0 };
// Raise the exception
__try { ::RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info ); }
__except( EXCEPTION_EXECUTE_HANDLER ) {}
}
bool NTTask::StartInternal()
{
if (m_Handle)
{
assert(false); // This may be lifted... just want to see if it happens
Stop();
}
m_Handle = ::CreateThread( NULL, m_stackSize, (LPTHREAD_START_ROUTINE)thread_proc, (void*)this, NULL, &m_ID );
if (m_ID!=NULL)
set_thread_name(m_taskName,m_ID);
return m_Handle!=NULL;
}
/**
* Starts this task.
* If it is already running or unable to start, it fails and returns false.
*/
bool NTTask::Start(void *arg0)
{
m_Arg=arg0;
return StartInternal();
}
/**
* Restarts a running task.
* If the task isn't started, it starts it.
* @return false if the task is running and we are unable to kill the previous instance
*/
bool NTTask::Restart()
{
//return HandleError(taskRestart(m_taskID));
Stop();
return StartInternal();
}
/**
* Kills the running task.
* @returns true on success false if the task doesn't exist or we are unable to kill it.
*/
bool NTTask::Stop()
{
if (!m_Handle) return false;
bool ok = true;
// Wait for the thread to finish
#ifdef _DEBUG
try_again:
#endif _DEBUG
//const int TimeOut=2000;
const int TimeOut=INFINITE;
if ( ::WaitForSingleObject( m_Handle , TimeOut ) == WAIT_TIMEOUT )
{ // Signal the thread as having been terminated
//if ( m_p_error ) *m_p_error = true;
// If this gets triggered we have a bug in the code.
#ifdef _DEBUG
switch( ::MessageBoxW( NULL, L"A thread being used by the application\n"
L"has taken to long to exit and so is about\n"
L"to be terminated to avoid locking-up\n"
L"the application.\n\n"
L"Click ABORT to debug.\n"
L"Click RETRY to wait for a bit longer.\n"
L"Click IGNORE to terminate the thread.\n\n"
L"This message is NOT displayed in release mode.",
L"Thread exit has timed out.",
MB_ABORTRETRYIGNORE ) )
{ case IDRETRY: goto try_again;
case IDABORT: ::DebugBreak(); break;
case IDIGNORE: break;
}
#endif _DEBUG
// Free thread memory
CONTEXT c_ = {0};
c_.ContextFlags = CONTEXT_FULL;
::GetThreadContext( m_Handle, &c_ );
MEMORY_BASIC_INFORMATION Info_ = {0};
#ifdef _M_X64
::VirtualQuery( (PVOID) c_.Rsp, &Info_, sizeof(Info_) );
#else
::VirtualQuery( (PVOID) c_.Esp, &Info_, sizeof(Info_) );
#endif
// Terminate the thread
::TerminateThread( m_Handle, 0 );
// Free the memory
::VirtualFree( Info_.AllocationBase, 0, MEM_RELEASE );
}
//if (Verify())
//{
// ok = HandleError(taskDelete(m_taskID));
//}
//m_taskID = kInvalidTaskID;
// The thread has finished
CloseHandle( m_Handle );
m_Handle = NULL;
return ok;
}
/**
* Returns true if the task is ready to execute (i.e. not suspended, delayed, or blocked).
* @return true if ready, false if not ready.
*/
bool NTTask::IsReady()
{
//return taskIsReady(m_taskID);
return m_Handle!=NULL;
}
/**
* Returns true if the task was explicitly suspended by calling Suspend()
* @return true if suspended, false if not suspended.
*/
bool NTTask::IsSuspended()
{
//return taskIsSuspended(m_taskID);
return false;
}
/**
* Pauses a running task.
* Returns true on success, false if unable to pause or the task isn't running.
*/
bool NTTask::Suspend()
{
//return HandleError(taskSuspend(m_taskID));
assert(false);
return false;
}
/**
* Resumes a paused task.
* Returns true on success, false if unable to resume or if the task isn't running/paused.
*/
bool NTTask::Resume()
{
//return HandleError(taskResume(m_taskID));
assert(false);
return false;
}
/**
* Verifies a task still exists.
* @returns true on success.
*/
bool NTTask::Verify()
{
//return taskIdVerify(m_taskID) == OK;
return true;
}
/**
* Gets the priority of a task.
* @returns task priority or 0 if an error occured
*/
INT32 NTTask::GetPriority()
{
//if (HandleError(taskPriorityGet(m_taskID, &m_priority)))
// return m_priority;
//else
// return 0;
return m_priority;
}
/**
* This routine changes a task's priority to a specified priority.
* Priorities range from 0, the highest priority, to 255, the lowest priority.
* Default task priority is 100.
* @param priority The priority the task should run at.
* @returns true on success.
*/
bool NTTask::SetPriority(INT32 priority)
{
m_priority = priority;
//return HandleError(taskPrioritySet(m_taskID, m_priority));
return true;
}
/**
* Returns the name of the task.
* @returns Pointer to the name of the task or NULL if not allocated
*/
const char* NTTask::GetName()
{
return m_taskName;
}
/**
* Get the ID of a task
* @returns Task ID of this task. NTTask::kInvalidTaskID (-1) if the task has not been started or has already exited.
*/
INT32 NTTask::GetID()
{
//if (Verify())
// return m_taskID;
if (m_Handle)
return m_ID;
return kInvalidTaskID;
}
/**
* Handles errors generated by task related code.
*/
bool NTTask::HandleError(STATUS results)
{
if (results != ERROR) return true;
//switch(errnoGet())
//{
//case S_objLib_OBJ_ID_ERROR:
// wpi_setWPIErrorWithContext(TaskIDError, m_taskName);
// break;
//
//case S_objLib_OBJ_DELETED:
// wpi_setWPIErrorWithContext(TaskDeletedError, m_taskName);
// break;
//
//case S_taskLib_ILLEGAL_OPTIONS:
// wpi_setWPIErrorWithContext(TaskOptionsError, m_taskName);
// break;
//
//case S_memLib_NOT_ENOUGH_MEMORY:
// wpi_setWPIErrorWithContext(TaskMemoryError, m_taskName);
// break;
//
//case S_taskLib_ILLEGAL_PRIORITY:
// wpi_setWPIErrorWithContext(TaskPriorityError, m_taskName);
// break;
//default:
// printErrno(errnoGet());
// wpi_setWPIErrorWithContext(TaskError, m_taskName);
//}
return false;
}

View File

@@ -0,0 +1,109 @@
#include "stdafx.h"
/*
* FDIOStream.cpp
*
* Created on: Sep 27, 2012
*/
#include "networktables2/stream/FDIOStream.h"
#include "networktables2/util/IOException.h"
#include "networktables2/util/EOFException.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winsock2.h>
#include <wininet.h>
#include <ws2tcpip.h>
FDIOStream::FDIOStream(int _fd){
fd = _fd;
}
FDIOStream::~FDIOStream(){
close();
}
int FDIOStream::read(void* ptr, int numbytes){
if(numbytes==0)
return 0;
char* bufferPointer = (char*)ptr;
int totalRead = 0;
while (totalRead < numbytes)
{
int numRead=recv(fd, bufferPointer, numbytes-totalRead, 0);
if(numRead == 0){
throw EOFException();
}
else if (numRead < 0) {
perror("read error: ");
fflush(stderr);
throw IOException("Error on FDIO read");
}
bufferPointer += numRead;
totalRead += numRead;
}
return totalRead;
}
int Send( int sockfd,char* Data, size_t sizeData )
{
assert(sockfd!=INVALID_SOCKET);
bool Result_ = true;
WSABUF wsaBuf_;
wsaBuf_.buf = Data;
wsaBuf_.len = (ULONG) sizeData;
DWORD BytesSent_;
while (WSASend( sockfd, &wsaBuf_, 1, &BytesSent_, 0, NULL, NULL ) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
Result_ = false;
break;
}
Sleep(1);
}
if (!Result_)
{
char Buffer[128];
sprintf(Buffer,"Send() failed: WSA error=%d\n",WSAGetLastError());
OutputDebugStringA(Buffer);
}
return(int)BytesSent_;
}
int FDIOStream::write(const void* ptr, int numbytes)
{
int numWrote = Send(fd,(char *)ptr,numbytes);
if(numWrote==numbytes)
return numWrote;
perror("write error: ");
fflush(stderr);
throw IOException("Could not write all bytes to fd stream");
}
void FDIOStream::flush(){
}
void FDIOStream::close()
{
//Note: the close includes to close the socket so that connection can be deferred deleted while immediately closing the socket for a new socket to open
if (fd != INVALID_SOCKET)
{
char Buffer[128];
sprintf(Buffer,"closesocket %d\n",fd);
OutputDebugStringA(Buffer);
shutdown( fd, SD_BOTH );
closesocket( fd );
fd = (int)INVALID_SOCKET; //pedantic, in case we cache as a member variable
Sleep(20); //give some time to take effect
}
}

View File

@@ -0,0 +1,123 @@
/*
* SocketServerStreamProvider.cpp
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#include "stdafx.h"
#include "networktables2/stream/SocketServerStreamProvider.h"
#include "networktables2/stream/FDIOStream.h"
#include "networktables2/util/IOException.h"
#include <cstring>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <wininet.h>
#include <ws2tcpip.h>
#ifndef ERROR
#define ERROR -1
#endif
typedef int addrlen_t;
static void load_tcpip(void)
{
WSAData wsaData_;
WORD wVersionRequested_ = MAKEWORD( 2, 2 );
int result=WSAStartup( wVersionRequested_, &wsaData_ );
assert(result==0);
}
static bool unload_tcpip(void)
{
WSACleanup();
return true;
}
SocketServerStreamProvider::SocketServerStreamProvider(int port)
{
load_tcpip();
struct sockaddr_in serverAddr;
int sockAddrSize = sizeof(serverAddr);
memset(&serverAddr, 0, sockAddrSize);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
throw IOException("Error creating server socket", errno);
}
// Set the TCP socket so that it can be reused if it is in the wait state.
int reuseAddr = 1;
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseAddr, sizeof(reuseAddr));
// Bind socket to local address.
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sockAddrSize) != 0)
{
close();
throw IOException("Could not bind server socket", errno);
}
if (listen(serverSocket, 1) == SOCKET_ERROR)
{
close();
throw IOException("Could not listen on server socket", errno);
}
}
SocketServerStreamProvider::~SocketServerStreamProvider()
{
close();
unload_tcpip();
}
IOStream* SocketServerStreamProvider::accept(){
struct timeval timeout;
// Check for a shutdown once per second
if (serverSocket!=INVALID_SOCKET)
{
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(serverSocket, &fdSet);
if (select(FD_SETSIZE, &fdSet, NULL, NULL, &timeout) > 0)
{
if (FD_ISSET(serverSocket, &fdSet))
{
//For windows these must be NULL to work properly
int connectedSocket = ::accept(serverSocket, NULL, NULL);
if (connectedSocket == INVALID_SOCKET)
return NULL;
int on = 1;
//Enable if we need high performance feedback
//setsockopt(connectedSocket, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
return new FDIOStream(connectedSocket);
}
}
}
return NULL;
}
void SocketServerStreamProvider::close()
{
if (serverSocket!=INVALID_SOCKET)
{
shutdown( serverSocket, SD_BOTH );
closesocket( serverSocket );
serverSocket = (int)INVALID_SOCKET;
}
}

View File

@@ -0,0 +1,136 @@
/*
* SocketStreamFactory.cpp
*
* Created on: Nov 3, 2012
* Author: Mitchell Wills
*/
#include "stdafx.h"
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include "networktables2/stream/FDIOStream.h"
#include "networktables2/stream/SocketStreamFactory.h"
static void load_tcpip(void)
{
WSAData wsaData_;
WORD wVersionRequested_ = MAKEWORD( 2, 2 );
int result=WSAStartup( wVersionRequested_, &wsaData_ );
assert(result==0);
}
static bool unload_tcpip(void)
{
WSACleanup();
return true;
}
SocketStreamFactory::SocketStreamFactory(const char* _host, int _port):host(_host), port(_port)
{
load_tcpip();
}
SocketStreamFactory::~SocketStreamFactory()
{
unload_tcpip();
}
IOStream *SocketStreamFactory::createStream(){
IOStream *ret=NULL;
int sockfd = INVALID_SOCKET;
try
{
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0) throw 0;
server = gethostbyname(host);
if (server == NULL)
throw 1;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(port);
//We are outgoing so setup the socket options before making the connection
//Setup for TCP_NODELAY for nice crisp response time...
int on = 1;
//Enable if we need high performance feedback
//setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
// Set the linger options
const LINGER linger = { 1, 0 };
setsockopt( sockfd, SOL_SOCKET, SO_LINGER, (const char *)&linger, sizeof(linger) );
//Note: on this last step... it can typically fail if there is no server present, so instead of issuing a throw I will handle the logic
//in place. This will avoid flooding debug output on a typical scenario
// [9/10/2013 JamesK]
{
char Buffer[128];
sprintf(Buffer,"connecting %d\n",sockfd);
OutputDebugStringA(Buffer);
}
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) == 0)
ret=new FDIOStream(sockfd);
else
{
if (sockfd != INVALID_SOCKET)
{
//We must shut down socket before issuing the close to avoid zombie ports (Ask Kirk)
shutdown( sockfd, SD_BOTH );
closesocket( sockfd );
sockfd = (int)INVALID_SOCKET; //pedantic, in case we cache as a member variable
}
}
}
catch (int ErrorCode)
{
//Close resources if we cannot meet all the preconditions
if (sockfd != INVALID_SOCKET)
{
//We must shut down socket before issuing the close to avoid zombie ports (Ask Kirk)
shutdown( sockfd, SD_BOTH );
closesocket( sockfd );
sockfd = (int)INVALID_SOCKET; //pedantic, in case we cache as a member variable
}
const char *ErrorMsg=NULL;
switch (ErrorCode)
{
case 0:
ErrorMsg="ERROR opening socket";
break;
case 1:
ErrorMsg="ERROR, no such host";
break;
case 2:
ErrorMsg="ERROR on connect";
break;
};
if (ErrorMsg)
{
char Buffer[1024];
sprintf(Buffer,"ErrorMsg=%s WSA error=%d\n",ErrorMsg,WSAGetLastError());
OutputDebugStringA(Buffer);
printf("ErrorMsg=%s WSA error=%d\n",ErrorMsg,WSAGetLastError());
//If we get a WSANOTINITIALISED error... try to reset it
if (WSAGetLastError()==WSANOTINITIALISED)
{
unload_tcpip();
Sleep(100); //just to be safe
load_tcpip();
//In my tests this actually works
}
}
Sleep(1000); //avoid flooding to connect... it doesn't need to occur every 20ms
}
return ret;
}

View File

@@ -0,0 +1,21 @@
/*
* SocketStreams.cpp
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#include "networktables2/stream/SocketStreams.h"
#include "networktables2/stream/SocketStreamFactory.h"
#include "networktables2/stream/SocketServerStreamProvider.h"
IOStreamFactory& SocketStreams::newStreamFactory(const char* host, int port){
return *new SocketStreamFactory(host, port);
}
IOStreamProvider& SocketStreams::newStreamProvider(int port){
return *new SocketServerStreamProvider(port);
}

View File

@@ -0,0 +1,62 @@
/*
* DefaultThreadManger.cpp
*
* Created on: Sep 21, 2012
* Author: Mitchell Wills
*/
#include "../../../../../../../../stdafx.h"
#include "networktables2/thread/DefaultThreadManager.h"
#include <stdio.h>
PeriodicNTThread::PeriodicNTThread(PeriodicRunnable* _r, const char* _name) :
name(_name), thread(new NTTask(name, (FUNCPTR)PeriodicNTThread::taskMain)), r(_r), run(true)
{
fprintf(stdout, "Starting task: %s\n", name);
fflush(stdout);
thread->Start(this);
}
PeriodicNTThread::~PeriodicNTThread()
{
stop();
//TODO somehow do this async
if (thread)
{
delete thread;
thread=NULL;
}
}
int PeriodicNTThread::taskMain(PeriodicNTThread* o)
{
//static wrapper
return o->_taskMain();
}
int PeriodicNTThread::_taskMain(){
try {
while(run){
r->run();
}
} catch (...) {
fprintf(stdout, "Task exited with uncaught exception %s\n", name);
fflush(stdout);
return 1;
}
fprintf(stdout, "Task exited normally: %s\n", name);
fflush(stdout);
return 0;
}
void PeriodicNTThread::stop()
{
run = false;
}
bool PeriodicNTThread::isRunning() {
return thread->IsReady();
}
NTThread* DefaultThreadManager::newBlockingPeriodicThread(PeriodicRunnable* r, const char* name) {
return new PeriodicNTThread(r, name);
}