Пример #1
0
int LoopCommand1(ThreadData* aThreadData)
	{	
	int j = 0,retval=0;
	struct timespec abstime;
	struct timeval now;
	gettimeofday(&now,NULL);
	
	abstime.tv_sec = now.tv_sec - 5;
	abstime.tv_nsec = now.tv_usec * 1000;
	
	for(int i=0; i < 1000; i += 100)
		{
		j++;
		if(j == 255)
			{
			j = 0;
			#ifdef WINDOWS
				printf("%dLooping Id:  %d Cmd1 %d Cmd2 %d\n",i,aData->iSelf,Command1, Command2);
			#else
				{
				//MSG(_L("%dLooping Id:  %d Cmd1 %d Cmd2 %d\n"),i,aData->iSelf,Command1, Command2);
				}
			#endif
			}
		retval = SemTimedWait(aThreadData,&abstime);
		retval = SemPost(aThreadData);
		}
	return retval;
	}
Пример #2
0
/*-----------------------------------------------------------------------------
|   Function        :   sflash_get_status
|
|   Parameters      :   OUT : unsigned char * : Status of Flash Device
|
|   Return Value    :   0 - on Success, Non-zero on failure
|
+----------------------------------------------------------------------------*/
int sflash_get_status( unsigned char *pStat)
{
    int iRetVal = SFLASH_SUCCESS;
    char temp[5];
    SCP_RW_STRUCT scp_rw;
//    int i;

    /* build the command     */
    memset(flash_cmd, 0, 296);
    
    flash_cmd[0] = S_FLASH_STATUS_CMD;
    flash_cmd[1] = S_FLASH_STATUS_CMD;
    flash_cmd[2] = 0x00;
    flash_cmd[3] = 0x00;
    flash_cmd[4] = 0x00;

    // setup the SCP_RW structure
    scp_rw.write_ptr = flash_cmd;
    scp_rw.read_ptr = temp;
    scp_rw.count = 4;
    
    /* Use SFLASH In Use Semaphore */
    SemWait(sSFlashInUseSem);

    if(ioctl(fd, IOCTL_SCP_RW, &scp_rw) != 0) 
    {
        printf("sflash_get_status failed with RC = %x \n", iRetVal);
        return -1;
    }
    *pStat = scp_rw.read_ptr[1];
    
    SemPost(sSFlashInUseSem);

    return 0;
}
Пример #3
0
int sflash_send_cmd(unsigned char * pInput, unsigned char * pOutput, unsigned long dwLen)
{
   SCP_RW_STRUCT scp_rw;
   int i;

   PDEBUG("sflash_send_cmd: pInput = 0x%8.8x, pOutput = 0x%8.8x, dwLen = %d\n", pInput, pOutput, dwLen);
  
    if(dwLen > SCP_BUFFER_SIZE)
   {
      printf("ERROR: sflash_send_cmd: dwLen 0x%x is larger than SCP_BUFFER_SIZE 0x%x\n",dwLen,SCP_BUFFER_SIZE);
      return -1;
   }   

    // setup the SCP_RW structure 
   scp_rw.write_ptr = pInput;
   scp_rw.read_ptr = pOutput;
   scp_rw.count = dwLen;

    /* Use SFLASH In Use Semaphore */
   SemWait(sSFlashInUseSem);

   if(ioctl(fd, IOCTL_SCP_RW, &scp_rw) != 0) 
   {
       printf("sflash_send_cmd failed.\n");
       return -1;
   }

   SemPost(sSFlashInUseSem);

   return 0;
}
Пример #4
0
error_code
threadlib_signal_sem(HSEM *e)
{
    if (!e)
	return SR_ERROR_INVALID_PARAM;
    SemPost(*e);
    return SR_SUCCESS;
}
static void dchild6( DREC *pdr, CHILD *pc )
{
    int rc;

    // Get the lock to synchronize with parent
    SemPend( hDSem6, SEM_FOREVER );
    SemPost( hDSem6 );

    // This function returns "1" if the socket is still open,
    // and "0" is the socket has been closed.
    rc = pdr->pCb( pc->s,  pdr->Argument );

    // Close the socket if we need to
    // We do this before we get the lock so if the socket
    // uses LINGER, we don't hold everyone up
    if( rc && pc->closeSock )
        fdClose( pc->s );

    // Get our lock
    SemPend( hDSem6, SEM_FOREVER );

    // Close the socket session (if open)
    if( pc->hTask )
        fdCloseSession( pc->hTask );

    // Remove our record from the DREC
    if( pc->pNext )
        pc->pNext->pPrev = pc->pPrev;
    if( !pc->pPrev )
        pdr->pC = pc->pNext;
    else
        pc->pPrev->pNext = pc->pNext;
    pdr->TasksSpawned--;

    // Free our record
    mmFree( pc );

    // Kick the parent thread
    if( hDTask6 )
        fdSelectAbort( hDTask6 );

    // Release the lock
    SemPost( hDSem6 );

    TaskExit();
}
Пример #6
0
void f(void* arg)
{
	int n = *((int*)arg);
	SemWait(&s);
	PutInt(n);
	PutString("\n");
	SemPost(&s);
	UserThreadExit(0);
}
Пример #7
0
void relacher_verrou()
{
    int error;
    // section critique
    error = SemPost(&sem);
    if(error != 0)
    {
        PutString("[mem.c] erreur semaphore post\n");
        Exit(1);
    }
}
/** 
 *  @b Description
 *  @n  
 *      The function is to used cleanup a previously created V6 daemon.
 *
 *  @param[in]  hEntry
 *      This is the handle to the daemon which was previously created 
 *      using Daemon6New
 *  @sa
 *      Daemon6New
 *
 *  @retval
 *      Not Applicable.
 */
void Daemon6Free( HANDLE hEntry )
{
    DREC *pdr = (DREC *)hEntry;
    CHILD *pc;

    // At this point we must have a semaphore
    if( !hDSem6 )
        return;

    // Enter our own lock
    SemPend( hDSem6, SEM_FOREVER );

    // Sanity check
    if( pdr->Type!=SOCK_STREAM && pdr->Type!=SOCK_DGRAM )
        goto errorout;

    // Clear the record
    pdr->Type = 0;
    RequestedRecords6--;

    // Close the socket session of all children. This will
    // cause them to eventually fall out of their code and
    // close their sockets
    pc = pdr->pC;
    while( pc )
    {
        if( pc->hTask )
        {
            fdCloseSession( pc->hTask );
            pc->hTask = 0;
        }
        pc = pc->pNext;
    }

    // Close the socket (this will wake anyone who's using it)
    if( pdr->s != INVALID_SOCKET )
    {
        fdClose( pdr->s );
        pdr->s = INVALID_SOCKET;
    }

    // If there are no more records, close the daemon task's
    // file descriptor session. That will cause it to error
    // out and remove itself
    if( !RequestedRecords6 )
    {
        fdCloseSession( hDTask6 );
        hDTask6 = 0;
    }

errorout:
    // Exit our lock
    SemPost( hDSem6 );
}
Пример #9
0
int LoopCommand2(ThreadData* aThreadData)
	{	
	int j = 0,retval=0;
	for(int i=0; i < 1000; i += 100)
		{
		j++;
		if(j == 255)
			{
			j = 0;
			#ifdef WINDOWS
				printf("%dLooping Id:  %d Cmd1 %d Cmd2 %d\n",i,aData->iSelf,Command1, Command2);
			#else
				{
				//MSG(_L("%dLooping Id:  %d Cmd1 %d Cmd2 %d\n"),i,aData->iSelf,Command1, Command2);
				}
			#endif
			}
		retval = SemPost(aThreadData);
		retval = SemWait(aThreadData);
		}
	return retval;
	}
Пример #10
0
int main()
{
	PutString("\n-----------------------------------------\n");
	PutString("Lancement du test testBcpThreads : \n");
	PutString("Lance plusieurs threads affichant un nombre different avec semaphore pour l'acces.\n");
	PutString("-----------------------------------------\n");
	int i;
	int nb[NB_THREADS];
	SemInit(&s, 1);
	for(i = 0 ; i < NB_THREADS ; i++)
	{
		nb[i] = i;
		if(UserThreadCreate(f, &nb[i]) == -1)
		{
			SemWait(&s);
			PutString("Echec de creation du thread : ");
			PutInt(i);
			PutString("\n");
			SemPost(&s);
		}
	}
	PutString("thread main se termine\n");
    return 0;
}
Пример #11
0
TInt CTestSemtrywait::TestSem392( )
	{
	
	int errsum=0, err = 0;
	int retval = 0;
	ThreadData lThreadData;

	sem_t lSignalSemaphore;
	sem_t lSuspendSemaphore;

	sem_t           lTestSemaphore;
	pthread_mutex_t lTestMutex;
	pthread_cond_t  lTestCondVar;
	pthread_condattr_t  lCondAttr;
	pthread_mutexattr_t lTestMutexAttr;

	pthread_mutexattr_t defaultattr;
	pthread_mutexattr_t errorcheckattr;
	pthread_mutexattr_t recursiveattr;

	pthread_mutexattr_init(&defaultattr);
	pthread_mutexattr_init(&errorcheckattr);
	pthread_mutexattr_init(&recursiveattr);

	pthread_mutexattr_settype(&errorcheckattr,PTHREAD_MUTEX_ERRORCHECK);
	pthread_mutexattr_settype(&recursiveattr,PTHREAD_MUTEX_RECURSIVE);


	pthread_mutex_t l_staticmutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_t l_errorcheckmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
	pthread_mutex_t l_recursivemutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
    pthread_cond_t  l_staticcondvar = PTHREAD_COND_INITIALIZER;


    CommonData lCommonData;
    lCommonData.iStaticMutex = &l_staticmutex;
	lCommonData.iErrorCheckMutex = &l_errorcheckmutex;
	lCommonData.iRecursiveMutex = &l_recursivemutex;
	lCommonData.iStaticCondVar = &l_staticcondvar;

	retval = sem_init(&lSignalSemaphore,0,0);
	if(retval != 0)
		{
		return retval;
		}

	retval = sem_init(&lSuspendSemaphore,0,0);
	if(retval != 0)
		{
		return retval;
		}

	lThreadData.iSignalSemaphore = &lSignalSemaphore;
	lThreadData.iSuspendSemaphore = &lSuspendSemaphore;
	lThreadData.iTestSemaphore   = &lTestSemaphore;
	lThreadData.iTestMutex       = &lTestMutex;
	lThreadData.iTestMutexAttr   = &lTestMutexAttr;
	lThreadData.iTestCondVar     = &lTestCondVar;
	lThreadData.iDefaultAttr     = &defaultattr;
	lThreadData.iErrorcheckAttr = &errorcheckattr;
	lThreadData.iRecursiveAttr   = &recursiveattr;

	lThreadData.iCondAttr        = &lCondAttr;
	for (int loop = 0; loop < EThreadMain; loop++)
		{
	    g_spinFlag[loop] = true;
		}
	lThreadData.iSuspending      = false;
	lThreadData.iSpinCounter     = 0;
	lThreadData.iCurrentCommand  = -1;
	lThreadData.iSelf            = EThreadMain;
	lThreadData.iValue           = 0;
	lThreadData.iRetValue        = 0;
	lThreadData.ierrno           = 0;
	lThreadData.iExpectederrno   = 0;
	lThreadData.iTimes           = 0;
	lThreadData.iStopped         = false;
	lThreadData.iCommonData      = &lCommonData;

	retval = SemInit(&lThreadData);
	
	fp=func2;
	retval = ThreadCreate(&lThreadData, (void*) EThread1);
	
	fp=func4;
	retval = ThreadCreate(&lThreadData, (void*) EThread2);

	retval = SemPost(&lThreadData);
	retval = ThreadDestroy(&lThreadData, (void*) EThread1);
	retval = ThreadDestroy(&lThreadData, (void*) EThread2);
	retval = SemDestroy(&lThreadData);
	StopThread(&lThreadData);
	
	err = pthread_cond_destroy(&l_staticcondvar);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_recursivemutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_errorcheckmutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_staticmutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutexattr_destroy(&recursiveattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
	err = pthread_mutexattr_destroy(&errorcheckattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
	err = pthread_mutexattr_destroy(&defaultattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = sem_destroy(&lSignalSemaphore);
	if(err != EINVAL)
		{	
		errsum += err;
		}
	err = sem_destroy(&lSuspendSemaphore);
	if(err != EINVAL)
		{
		errsum += err;
		}

	return retval+errsum;
	
	}
/** 
 *  @b Description
 *  @n  
 *      The function creates a V6 Daemon.
 *
 *  @param[in]  Type
 *      This is the type of socket being opened through the daemon. In
 *      the case of IPv6 the following types are supported
 *          - SOCK_STREAM
 *              Use this for TCP Sockets.
 *          - SOCK_DGRAM
 *              Use this for UDP Sockets.
 *  @param[in]  LocalAddress
 *      This is the Local Address to which the socket will be bound to.
 *      In most cases this is typically passed as IPV6_UNSPECIFIED_ADDRESS
 *  @param[in]  LocalPort
 *      This is the Local Port to serve (cannot be NULL)
 *  @param[in]  pCb
 *      Call back function which is to be invoked.
 *  @param[in]  Priority
 *      Priority of new task to create for callback function 
 *  @param[in]  StackSize
 *      Stack size of new task to create for callback function
 *  @param[in]  Argument
 *      Argument (besides socket) to pass to callback function
 *  @param[in]  MaxSpawn
 *      Maximum number of callback function instances (must be 1 for UDP)
 *
 *  @retval
 *      Success - Handle to the spawned Daemon.
 *  @retval
 *      Error   - 0
 */
HANDLE Daemon6New (uint Type, IP6N LocalAddress, uint LocalPort, int (*pCb)(SOCKET,UINT32),
                   uint Priority, uint StackSize, UINT32 Argument, uint MaxSpawn )
{
    int i;
    DREC *pdr = 0;

    // Sanity check the arguments
    if( Type==SOCK_DGRAM )
        MaxSpawn=1;
    else if( Type!=SOCK_STREAM)
        return(0);

    if( !LocalPort || !pCb || Priority<1 || Priority>15 || !StackSize || !MaxSpawn )
        return(0);

    // We'll borrow the stack's kernel mode for a temp exclusion method
    llEnter();
    if( !hDSem6 )
    {
        hDSem6 = SemCreate( 1 );
        bzero( drec6, sizeof(drec6) );
        RequestedRecords6 = 0;
    }
    llExit();

    // At this point we must have a semaphore
    if( !hDSem6 )
        return(0);

    // Enter our own lock
    SemPend( hDSem6, SEM_FOREVER );

    // Scan the list for a free slot
    for( i=0; i<DAEMON_MAXRECORD; i++ )
        if( !drec6[i].Type && !drec6[i].TasksSpawned )
            break;

    // Break out if no free records
    if(i==DAEMON_MAXRECORD)
        goto errorout;

    // Build the new record
    pdr = &drec6[i];
    pdr->Type           = Type;
    pdr->LocalV6Address = LocalAddress;
    pdr->LocalPort      = LocalPort;
    pdr->pCb            = pCb;
    pdr->Priority       = Priority;
    pdr->StackSize      = StackSize;
    pdr->Argument       = Argument;
    pdr->MaxSpawn       = MaxSpawn;
    pdr->s              = INVALID_SOCKET;

    // If the Deamon task exists, ping it, otherwise create it
    if( hDTask6 )
        fdSelectAbort( hDTask6 );
    else
    {
        hDTask6 = TaskCreate(daemon6,"daemon6",OS_TASKPRINORM,OS_TASKSTKLOW,0,0,0);
        if( hDTask6 )
            fdOpenSession(hDTask6);
        else
        {
            pdr->Type = 0;
            pdr = 0;
            goto errorout;
        }
    }

    RequestedRecords6++;

errorout:
    // Exit our lock
    SemPost( hDSem6 );
    return( pdr );
}
static void daemon6()
{
    int                   i,closeSock;
    struct sockaddr_in6   sin1;
    SOCKET                tsock;
    CHILD*                pc;

    // Enter our lock
    SemPend( hDSem6, SEM_FOREVER );

    for(;;)
    {
        //
        // Create any socket that needs to be created
        //
        for( i=0; i<DAEMON_MAXRECORD; i++ )
        {
            if( drec6[i].Type && drec6[i].s == INVALID_SOCKET )
            {
                // Create UDP or TCP as needed
                if( drec6[i].Type == SOCK_DGRAM )
                    drec6[i].s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
                else
                    drec6[i].s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);

                // If the socket was created, bind it
                if( drec6[i].s != INVALID_SOCKET )
                {
                    // Bind to the specified Server port
                    bzero( &sin1, sizeof(struct sockaddr_in) );
                    sin1.sin6_family     = AF_INET6;
                    memcpy((void *)&sin1.sin6_addr,(void *)&drec6[i].LocalAddress, sizeof(struct in6_addr));
                    sin1.sin6_port       = htons(drec6[i].LocalPort);

                    if( bind( drec6[i].s,(struct sockaddr *)&sin1, sizeof(sin1) ) < 0 )
                    {
                        fdClose( drec6[i].s );
                        drec6[i].s = INVALID_SOCKET;
                    }
                }

                // If the socket is bound and TCP, start listening
                if( drec6[i].s != INVALID_SOCKET && drec6[i].Type != SOCK_DGRAM )
                {
                    if( listen( drec6[i].s, drec6[i].MaxSpawn ) < 0 )
                    {
                        fdClose( drec6[i].s );
                        drec6[i].s = INVALID_SOCKET;
                    }
                }
            }

            // Update the fdPoll array
            pollitem6[i].fd = drec6[i].s;
            if( drec6[i].Type && drec6[i].TasksSpawned < drec6[i].MaxSpawn )
                pollitem6[i].eventsRequested = POLLIN;
            else
                pollitem6[i].eventsRequested = 0;
        }

        // Leave our lock
        SemPost( hDSem6 );

        // Poll with a timeout of 10 second - to try and catch
        // synchronization error
        if( fdPoll( pollitem6, DAEMON_MAXRECORD, 10000 ) == SOCKET_ERROR )
            break;

        // Enter our lock
        SemPend( hDSem6, SEM_FOREVER );

        //
        // Spawn tasks for any active sockets
        //
        for( i=0; i<DAEMON_MAXRECORD; i++ )
        {
            // If no poll results or the drec6 has been freed, skip it
            if( !pollitem6[i].eventsDetected || !drec6[i].Type )
                continue;

            // If the socket is invalid, close it
            if( pollitem6[i].eventsDetected & POLLNVAL )
            {
                fdClose( drec6[i].s);
                drec6[i].s = INVALID_SOCKET;
                continue;
            }

            if( pollitem6[i].eventsDetected & POLLIN )
            {
                if( drec6[i].Type == SOCK_DGRAM )
                {
                    tsock = drec6[i].s;
                    closeSock = 0;
                }
                else
                {
                    tsock = accept( drec6[i].s, 0, 0 );
                    closeSock = 1;
                }

                if( tsock != INVALID_SOCKET )
                {
                    // Create a record to track this task
                    pc = mmAlloc( sizeof(CHILD) );
                    if( !pc )
                        goto spawnComplete;

                    // Create the task
                    pc->hTask = TaskCreate( dchild6, "dchild6",
                                            drec6[i].Priority, drec6[i].StackSize,
                                            (UINT32)&drec6[i], (UINT32)pc, 0);
                    if( !pc->hTask )
                    {
                        mmFree( pc );
                        goto spawnComplete;
                    }

                    // Open a socket session for the child task
                    fdOpenSession( pc->hTask );

                    // Fill in the rest of the child record
                    pc->closeSock = closeSock;
                    pc->s = tsock;

                    // Now we won't close the socket here
                    closeSock = 0;

                    // Link this record onto the DREC
                    drec6[i].TasksSpawned++;
                    pc->pPrev = 0;
                    pc->pNext = drec6[i].pC;
                    drec6[i].pC = pc;
                    if( pc->pNext )
                        pc->pNext->pPrev = pc;

spawnComplete:
                    // If there was an error, we may need to close the socket
                    if( closeSock )
                        fdClose( tsock );
                }
            }
        }
    }

    TaskExit();
}