Exemplo n.º 1
0
const void *FakeSMCKey::getValue() 
{ 
	if (handler) {
        mach_timespec_t now, end;

        end.tv_sec = lastUpdated.tv_sec;
        end.tv_nsec = lastUpdated.tv_nsec;
        now.tv_sec = 1;
        now.tv_nsec = 0;
        
        ADD_MACH_TIMESPEC(&end, &now);
        
        clock_get_system_nanotime((clock_sec_t*)&now.tv_sec, (clock_nsec_t*)&now.tv_nsec);
        
        if (CMP_MACH_TIMESPEC(&end, &now) < 0) {            
            IOReturn result = handler->callPlatformFunction(kFakeSMCGetValueCallback, false, (void *)key, (void *)value, (void *)size, 0);
            
            if (kIOReturnSuccess == result)
                clock_get_system_nanotime((clock_sec_t*)&lastUpdated.tv_sec, (clock_nsec_t*)&lastUpdated.tv_nsec);
            else 
                HWSensorsWarningLog("value update request callback error for key %s, return 0x%x", key, result);
        }
	}
    
	return value; 
};
Exemplo n.º 2
0
/**
 * __i2c_transfer - unlocked flavor of i2c_transfer
 * @adap: Handle to I2C bus
 * @msgs: One or more messages to execute before STOP is issued to
 *	terminate the operation; each message begins with a START.
 * @num: Number of messages to be executed.
 *
 * Returns negative errno, else the number of messages executed.
 *
 * Adapter lock must be held when calling this function. No debug logging
 * takes place. adap->algo->master_xfer existence isn't checked.
 */
int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
    int ret, try1;
    
	/* Retry automatically on arbitration loss */
    mach_timespec_t end, now;
    
    clock_get_system_nanotime((clock_sec_t*)&end.tv_sec, (clock_usec_t*)&end.tv_nsec);
    
    now.tv_sec = 0;
    now.tv_nsec = adap->timeout * NSEC_PER_USEC;
    
    ADD_MACH_TIMESPEC(&end, &now);
    
	for (ret = 0, try1 = 0; try1 <= adap->retries; try1++) {
		ret = adap->algo->master_xfer(adap, msgs, num);
        
        IOLog("GeforceSensors: _i2c_transfer=%d\n", ret);
        
		if (ret != -EAGAIN)
			break;
        
        clock_get_system_nanotime((clock_sec_t*)&now.tv_sec, (clock_nsec_t*)&now.tv_nsec);
        
		if (CMP_MACH_TIMESPEC(&end, &now) <= 0)
			break;
	}
    
	return ret;
}
Exemplo n.º 3
0
    // Mac OS X
    static void * thread_func(void * arg) {
        scoped_timer::imp * st = static_cast<scoped_timer::imp*>(arg);  

        pthread_mutex_t  mutex;
        clock_serv_t host_clock;
        struct timespec abstime;
        mach_timespec_t now;
        unsigned long long nano = static_cast<unsigned long long>(st->m_interval) * 1000000ull;

        host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock);

        if (pthread_mutex_init(&mutex, NULL) != 0)
            throw default_exception("failed to initialize timer mutex");
        if (pthread_cond_init(&st->m_condition_var, NULL) != 0)
            throw default_exception("failed to initialize timer condition variable");

        abstime.tv_sec  = nano / 1000000000ull;
        abstime.tv_nsec = nano % 1000000000ull;

        pthread_mutex_lock(&mutex);
        clock_get_time(host_clock, &now);
        ADD_MACH_TIMESPEC(&abstime, &now);
        int e = pthread_cond_timedwait(&st->m_condition_var, &mutex, &abstime);
        if (e != 0 && e != ETIMEDOUT)
            throw default_exception("failed to start timed wait");
        st->m_eh->operator()();
        pthread_mutex_unlock(&mutex);

        if (pthread_mutex_destroy(&mutex) != 0)
            throw default_exception("failed to destroy pthread mutex");
        if (pthread_cond_destroy(&st->m_condition_var) != 0)
            throw default_exception("failed to destroy pthread condition variable");
        return st;
    }
Exemplo n.º 4
0
float PTIDSensors::readTachometer(UInt32 index)
{
    mach_timespec_t now;
    
    clock_get_system_nanotime((clock_sec_t*)&now.tv_sec, (clock_nsec_t*)&now.tv_nsec);
    
    if (CMP_MACH_TIMESPEC(&tachometerNextUpdate, &now) <= 0) {
        mach_timespec_t next;
        
        tachometerNextUpdate.tv_sec = now.tv_sec;
        tachometerNextUpdate.tv_nsec = now.tv_nsec;
        next.tv_sec = 1;
        next.tv_nsec = 0;
        
        ADD_MACH_TIMESPEC(&tachometerNextUpdate, &next);
        
        updateTachometers();
    }
    
    if (tachometers) {
        if (OSNumber *number = OSDynamicCast(OSNumber, tachometers->getObject(index))) {
            UInt64 value = number->unsigned32BitValue();
            return (value == 0x80000000) ? 0 : (float)value;
        }
    }
    
    return 0;
}
Exemplo n.º 5
0
float INT340EMonitor::readTemperature(UInt32 index)
{
    mach_timespec_t now;

    clock_get_system_nanotime((clock_sec_t*)&now.tv_sec, (clock_nsec_t*)&now.tv_nsec);

    if (CMP_MACH_TIMESPEC(&temperatureNextUpdate, &now) <= 0) {
        mach_timespec_t next;

        temperatureNextUpdate.tv_sec = now.tv_sec;
        temperatureNextUpdate.tv_nsec = now.tv_nsec;
        next.tv_sec = 1;
        next.tv_nsec = 0;

        ADD_MACH_TIMESPEC(&temperatureNextUpdate, &next);

        updateTemperatures();
    }

    if (temperatures) {
        if (OSNumber *number = OSDynamicCast(OSNumber, temperatures->getObject(index))) {
            UInt64 value = number->unsigned32BitValue();
            return (value == 0x80000000) ? 0 : (float)((value - 0xAAC) / 0xA);
        }
    }

    return 0;
}
Exemplo n.º 6
0
int
nanosleep(const struct timespec *requested_time, struct timespec *remaining_time) {
    kern_return_t kret;
    int ret;
    mach_timespec_t current;
    mach_timespec_t completion;
   
	if (__unix_conforming == 0)
		__unix_conforming = 1;

#ifdef VARIANT_CANCELABLE
    pthread_testcancel();
#endif /* VARIANT_CANCELABLE */

    if ((requested_time == NULL) || (requested_time->tv_sec < 0) || (requested_time->tv_nsec >= NSEC_PER_SEC)) {
        errno = EINVAL;
        return -1;
    }


    if (remaining_time != NULL) {
	/* once we add requested_time, this will be the completion time */
        kret = clock_get_time(clock_port, &completion);
        if (kret != KERN_SUCCESS) {
            fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(kret));
            errno = EINVAL;
            return -1;
        }
    }
    ret = SEMWAIT_SIGNAL(clock_sem, MACH_PORT_NULL, 1, 1, (int64_t)requested_time->tv_sec, (int32_t)requested_time->tv_nsec);    
    if (ret < 0) {
        if (errno == ETIMEDOUT) {
		return 0;
        } else if (errno == EINTR) {
            if (remaining_time != NULL) {
                ret = clock_get_time(clock_port, &current);
                if (ret != KERN_SUCCESS) {
                    fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(ret));
                    return -1;
                }
                /* This depends on the layout of a mach_timespec_t and timespec_t being equivalent */
                ADD_MACH_TIMESPEC(&completion, requested_time);
		/* We have to compare first, since mach_timespect_t contains unsigned integers */
		if(CMP_MACH_TIMESPEC(&completion, &current) > 0) {
		    SUB_MACH_TIMESPEC(&completion, &current);
		    remaining_time->tv_sec = completion.tv_sec;
		    remaining_time->tv_nsec = completion.tv_nsec;
		} else {
		    bzero(remaining_time, sizeof(*remaining_time));
		}
            }
        } else {
            errno = EINVAL;
	}
    }
    return -1;
}
Exemplo n.º 7
0
    imp(unsigned ms, event_handler * eh):
        m_eh(eh) {
#if defined(_WINDOWS) || defined(_CYGWIN)
        m_first = true;
        CreateTimerQueueTimer(&m_timer,
                              NULL,
                              abort_proc,
                              this,
                              0,
                              ms,
                              WT_EXECUTEINTIMERTHREAD);
#elif defined(__APPLE__) && defined(__MACH__)
        // Mac OS X
        m_interval = ms?ms:0xFFFFFFFF;
        if (pthread_attr_init(&m_attributes) != 0)
            throw default_exception("failed to initialize timer thread attributes");
        if (pthread_cond_init(&m_condition_var, NULL) != 0)
            throw default_exception("failed to initialize timer condition variable");
        if (pthread_mutex_init(&m_mutex, NULL) != 0)
            throw default_exception("failed to initialize timer mutex");

        clock_serv_t host_clock;
        mach_timespec_t now;
        unsigned long long nano = static_cast<unsigned long long>(m_interval) * 1000000ull;
        
        host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock);
        m_end_time.tv_sec  = nano / 1000000000ull;
        m_end_time.tv_nsec = nano % 1000000000ull;
        clock_get_time(host_clock, &now);
        ADD_MACH_TIMESPEC(&m_end_time, &now);


        if (pthread_create(&m_thread_id, &m_attributes, &thread_func, this) != 0)
            throw default_exception("failed to start timer thread");
#elif defined(_LINUX_) || defined(_FREEBSD_)
        // Linux & FreeBSD
        struct sigevent sev;
        memset(&sev, 0, sizeof(sigevent));
        sev.sigev_notify = SIGEV_THREAD;
        sev.sigev_value.sival_ptr = this;
        sev.sigev_notify_function = sig_handler;
        if (timer_create(CLOCKID, &sev, &m_timerid) == -1)
            throw default_exception("failed to create timer");

        unsigned long long nano = static_cast<unsigned long long>(ms) * 1000000ull;
        struct itimerspec its;
        its.it_value.tv_sec  = nano / 1000000000ull;
        its.it_value.tv_nsec = nano % 1000000000ull;
        its.it_interval.tv_sec  = 0; // timer experies once
        its.it_interval.tv_nsec = 0;
        
        if (timer_settime(m_timerid, 0, &its, NULL) == -1)
            throw default_exception("failed to set timer");
#else
    // Other platforms
#endif
    }