Ejemplo n.º 1
0
static void registerUnregisterFds() {
  int fd, fdMax;

  FD_ZERO(&curlSrc->fdRead);
  FD_ZERO(&curlSrc->fdWrite);
  FD_ZERO(&curlSrc->fdExc);
  curlSrc->fdMax = -1;
  /* What fds does libcurl want us to poll? */
  curl_multi_fdset(curlSrc->multiHandle, &curlSrc->fdRead,
                   &curlSrc->fdWrite, &curlSrc->fdExc, &curlSrc->fdMax);
  if ((curlSrc->fdMax < -1) || (curlSrc->fdMax > GLIBCURL_FDMAX)) {
	  DEBUG_FAILURE(("registerUnregisterFds: fdMax=%d\n", curlSrc->fdMax));
  }
  /*fprintf(stderr, "registerUnregisterFds: fdMax=%d\n", curlSrc->fdMax);*/
  assert(curlSrc->fdMax >= -1 && curlSrc->fdMax <= GLIBCURL_FDMAX);

  fdMax = curlSrc->fdMax;
  if (fdMax < curlSrc->lastPollFdMax) fdMax = curlSrc->lastPollFdMax;

  /* Has the list of required events for any of the fds changed? */
  for (fd = 0; fd <= fdMax; ++fd) {
    gushort events = 0;
    if (FD_ISSET(fd, &curlSrc->fdRead))  events |= GLIBCURL_READ;
    if (FD_ISSET(fd, &curlSrc->fdWrite)) events |= GLIBCURL_WRITE;
    if (FD_ISSET(fd, &curlSrc->fdExc))   events |= GLIBCURL_EXC;

    /* List of events unchanged => no (de)registering */
    if (events == curlSrc->lastPollFd[fd].events) continue;

    DD((stdout, "registerUnregisterFds: fd %d: old events %x, "
       "new events %x\n", fd, curlSrc->lastPollFd[fd].events, events));

    /* fd is already a lastPollFd, but event type has changed => do nothing.
       Due to the implementation of g_main_context_query(), the new event
       flags will be picked up automatically. */
    if (events != 0 && curlSrc->lastPollFd[fd].events != 0) {
      curlSrc->lastPollFd[fd].events = events;
      continue;
    }
    curlSrc->lastPollFd[fd].events = events;

    /* Otherwise, (de)register as appropriate */
    if (events == 0) {
      g_source_remove_poll(&curlSrc->source, &curlSrc->lastPollFd[fd]);
      curlSrc->lastPollFd[fd].revents = 0;
      D((stderr, "unregister fd %d\n", fd));
    } else {
      g_source_add_poll(&curlSrc->source, &curlSrc->lastPollFd[fd]);
      D((stderr, "register fd %d\n", fd));
    }
  }

  curlSrc->lastPollFdMax = curlSrc->fdMax;
}
Ejemplo n.º 2
0
static gboolean source_dispatch(GSource *source, GSourceFunc callback,
	gpointer data)
{
	struct timeval zerotimeout = {
		.tv_sec = 0,
		.tv_usec = 0,
	};

	/* FIXME error handling */
	fp_handle_events_timeout(&zerotimeout);

	/* FIXME whats the return value used for? */
	return TRUE;
}

static void source_finalize(GSource *source)
{
	struct fdsource *_fdsource = (struct fdsource *) source;
	GSList *elem = _fdsource->pollfds;

	if (elem)
		do {
			GPollFD *pollfd = elem->data;
			g_source_remove_poll((GSource *) _fdsource, pollfd);
			g_slice_free(GPollFD, pollfd);
			_fdsource->pollfds = g_slist_delete_link(_fdsource->pollfds, elem);
		} while ((elem = g_slist_next(elem)));

	g_slist_free(_fdsource->pollfds);
}

static GSourceFuncs sourcefuncs = {
	.prepare = source_prepare,
	.check = source_check,
	.dispatch = source_dispatch,
	.finalize = source_finalize,
};

struct fdsource *fdsource = NULL;

static void pollfd_add(int fd, short events)
{
	GPollFD *pollfd = g_slice_new(GPollFD);
	pollfd->fd = fd;
	pollfd->events = 0;
	pollfd->revents = 0;
	if (events & POLLIN)
		pollfd->events |= G_IO_IN;
	if (events & POLLOUT)
		pollfd->events |= G_IO_OUT;

	fdsource->pollfds = g_slist_prepend(fdsource->pollfds, pollfd);
	g_source_add_poll((GSource *) fdsource, pollfd);
}
Ejemplo n.º 3
0
static void
g_mdns_user_data_destroy (GMDNSUserData *ud)
{
	g_return_if_fail (ud);

	g_source_remove_poll (ud->source, ud->fd);
	g_free (ud->fd);
	g_source_destroy (ud->source);
	DNSServiceRefDeallocate (ud->client);
	g_free (ud);
}
Ejemplo n.º 4
0
static void glib_io_free(pa_io_event*e) {
    g_assert(e);
    g_assert(!e->dead);

    e->dead = 1;
    e->mainloop->io_events_please_scan++;

    if (e->poll_fd_added) {
        g_source_remove_poll(&e->mainloop->source, &e->poll_fd);
        e->poll_fd_added = 0;
    }
}
Ejemplo n.º 5
0
static void watch_free(AvahiWatch *w) {
    assert(w);
    assert(!w->dead);

    if (w->pollfd_added) {
        g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
        w->pollfd_added = FALSE;
    }

    w->dead = TRUE;
    w->glib_poll->timeout_req_cleanup = TRUE;
}
Ejemplo n.º 6
0
GCHSource*
G_main_add_IPC_Channel(int priority, IPC_Channel* ch
		       ,	gboolean can_recurse
		       ,	gboolean (*dispatch)(IPC_Channel* source_data,
						     gpointer        user_data)
		       ,	gpointer userdata
		       ,	GDestroyNotify notify)
{
	GCHSource *chp;
	GSource *source;

	if( !ch ) {
		cl_log(LOG_WARNING, "%s:%d: got null channel", __FUNCTION__,__LINE__);
		return NULL;
	}
	source = g_source_new(&G_CH_SourceFuncs, 
					sizeof(GCHSource));
	G_main_IPC_Channel_constructor(source,ch,userdata,notify);
	
	chp = (GCHSource*)source;
	chp->dispatch = dispatch;
	
	g_source_set_priority(source, priority);
	g_source_set_can_recurse(source, can_recurse);
	
	chp->gsourceid = g_source_attach(source, NULL);
	chp->description = "IPC channel";
	

	if (chp->gsourceid == 0) {
		g_source_remove_poll(source, &chp->infd);
		if (!chp->fd_fdx) {
			g_source_remove_poll(source, &chp->outfd);
		}
		g_source_unref(source);
		source = NULL;
		chp = NULL;
	}
	return chp;
}
Ejemplo n.º 7
0
static gboolean g_source_fd_dispatch (GSource    *source,
                                      GSourceFunc callback,
                                      gpointer    user_data)
{
  GSourceFD *sfd = (GSourceFD *) source;
  if (callback != NULL
   && !callback (user_data))
    {
      g_source_remove_poll (source, &sfd->poll_fd);
      sfd->has_poll = FALSE;
      return FALSE;
    }
  return TRUE;
}
Ejemplo n.º 8
0
/*
 *	Add an IPC_WaitConnection to the gmainloop world...
 */
GWCSource*
G_main_add_IPC_WaitConnection(int priority
,	IPC_WaitConnection* wch
,	IPC_Auth* auth_info
,	gboolean can_recurse
,	gboolean (*dispatch)(IPC_Channel* wch
,		gpointer        user_data)
,	gpointer userdata
,	GDestroyNotify notify)
{

	GWCSource* wcp;
	GSource * source = g_source_new(&G_WC_SourceFuncs, 
					sizeof(GWCSource));
	
	wcp = (GWCSource*)source;
	
	wcp->magno = MAG_GWCSOURCE;
	wcp->maxdispatchdelayms = DEFAULT_MAXDELAY;
	wcp->maxdispatchms = DEFAULT_MAXDISPATCH;
	lc_store((wcp->detecttime), zero_longclock);
	wcp->udata = userdata;
	wcp->gpfd.fd = wch->ops->get_select_fd(wch);
	wcp->gpfd.events = DEF_EVENTS;
	wcp->gpfd.revents = 0;
	wcp->wch = wch;
	wcp->dnotify = notify;
	wcp->auth_info = auth_info;
	wcp->dispatch = dispatch;
	
	g_source_add_poll(source, &wcp->gpfd);
	
	g_source_set_priority(source, priority);
	
	g_source_set_can_recurse(source, can_recurse);
	
	wcp->gsourceid = g_source_attach(source, NULL);
	wcp->description = "IPC wait for connection";
	
	if (wcp->gsourceid == 0) {
		g_source_remove_poll(source, &wcp->gpfd);
		g_source_unref(source);
		source = NULL;
		wcp = NULL;
	}
	return wcp;
}
Ejemplo n.º 9
0
gboolean
G_main_del_fd(GFDSource* fdp)
{
	GSource * source = (GSource*) fdp;


	if (fdp->gsourceid <= 0) {
		return FALSE;
	}
	
	g_source_remove_poll(source, &fdp->gpfd);
	g_source_remove(fdp->gsourceid);
	fdp->gsourceid = 0;
	g_source_unref(source);
	
	return TRUE;

}
Ejemplo n.º 10
0
/** ========================================================================
 * The 'prepare' function of GSourceFuncs
 * 
 * @param base
 * @param timeout @see GSourceFuncs
 * 
 * @return gboolean @see GSourceFuncs
 * 
 * =========================================================================
 */
static gboolean
multi_fd_watch_prepare(GSource* base, gint* timeout)
{
    PSL_LOG_DEBUGLOW("%s (watch=%p)", __func__, base);

    PslMultiFdWatchSource* const source = (PslMultiFdWatchSource*)base;

    /// Clear revents of our pollrecs
    GHashTableIter iter;
    gpointer key, value;
    g_hash_table_iter_init(&iter, source->descTable);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        PslMultiFdDescriptor* const desc = (PslMultiFdDescriptor*)value;
        desc->pollFd.revents = 0;

        if (desc->pollFd.events) {
            if (!desc->pollAdded) {
                g_source_add_poll(base, &desc->pollFd);
                desc->pollAdded = true;
            }
        }
        else if (desc->pollAdded) {
            g_source_remove_poll(base, &desc->pollFd);
            desc->pollAdded = false;
        }

        PSL_LOG_DEBUGLOW(
            "%s (watch=%p): iter: table=%p, desc=%p, fd=%d, " \
            "monitoring GIOCondition=0x%lX",
            __func__, source, source->descTable, desc, (int)desc->pollFd.fd,
            (unsigned long)desc->pollFd.events);
    }

    /// Calc remaining timeout
    *timeout = multi_fd_watch_update_expiry_time(source);

    gboolean const readyNow = (0 == *timeout);


    PSL_LOG_DEBUGLOW("%s (watch=%p): %s", __func__, base,
                     readyNow ? "READY" : "NOT READY");
    return readyNow;
}
Ejemplo n.º 11
0
GFDSource*
G_main_add_fd(int priority, int fd, gboolean can_recurse
,	gboolean (*dispatch)(int fd, gpointer user_data)
,	gpointer userdata
,	GDestroyNotify notify)
{

	GSource* source = g_source_new(&G_fd_SourceFuncs, 
				       sizeof(GFDSource));
	GFDSource* ret = (GFDSource*)source;
	
	ret->magno = MAG_GFDSOURCE;
	ret->maxdispatchdelayms = DEFAULT_MAXDELAY;
	ret->maxdispatchms = DEFAULT_MAXDISPATCH;
	ret->udata = userdata;
	ret->dispatch = dispatch;
	ret->gpfd.fd = fd;
	ret->gpfd.events = DEF_EVENTS;
	ret->gpfd.revents = 0;
	ret->dnotify = notify;
	lc_store((ret->detecttime), zero_longclock);
	
	g_source_add_poll(source, &ret->gpfd);
	
	
	g_source_set_priority(source, priority);
	
	g_source_set_can_recurse(source, can_recurse);	
	
	ret->gsourceid = g_source_attach(source, NULL);
	ret->description = "file descriptor";
	
	if (ret->gsourceid == 0) {
		g_source_remove_poll(source, &ret->gpfd);
		memset(ret, 0, sizeof(GFDSource));
		g_source_unref(source);
		source = NULL;
		ret = NULL;
	}
	return ret;
}
Ejemplo n.º 12
0
void socketmanager::RegisterSockInterest(CURL *e, curl_socket_t s, int what) {
	sock_gsource *sgs = (sock_gsource *) gs;
	short events = 0;
	switch (what) {
		case CURL_POLL_NONE:
		case CURL_POLL_REMOVE:
		default:
			events = 0;
			break;

		case CURL_POLL_IN:
			events = G_IO_IN | G_IO_PRI | G_IO_ERR;
			break;

		case CURL_POLL_OUT:
			events = G_IO_OUT | G_IO_ERR;
			break;

		case CURL_POLL_INOUT:
			events = G_IO_IN | G_IO_PRI | G_IO_OUT | G_IO_ERR;
			break;
	}
	auto egp = sgs->sm->sockpollmap.find(s);
	GPollFD *gpfd = nullptr;
	if (egp != sgs->sm->sockpollmap.end()) {
		gpfd = &(egp->second);
		g_source_remove_poll(sgs, gpfd);
		if (!events) {
			sgs->sm->sockpollmap.erase(egp);
		}
	} else if (events) {
		gpfd = &sgs->sm->sockpollmap[s];
	}
	if (events) {
		gpfd->fd = s;
		gpfd->events = events;
		gpfd->revents = 0;
		g_source_add_poll(sgs, gpfd);
	}
}
Ejemplo n.º 13
0
/*
 *	Someone is trying to connect.
 *	Try to accept the connection and notify the user.
 */
static gboolean
G_WC_dispatch(GSource* source,
	      GSourceFunc callback,
	      gpointer user_data)
{
	GWCSource* wcp = (GWCSource*)source;
	IPC_Channel*	ch;
	gboolean	rc = TRUE;
	int		count = 0;
	longclock_t	dispstart;
	
	g_assert(IS_WCSOURCE(wcp));
	CHECK_DISPATCH_DELAY(wcp);
	
        while(1) {
		ch = wcp->wch->ops->accept_connection(wcp->wch, wcp->auth_info);
		if (ch == NULL) {
			if (errno == EBADF) {
				cl_perror("%s: Stopping accepting connections(socket=%d)!!"
				,	__FUNCTION__, wcp->gpfd.fd);
				rc = FALSE;
			}
			break;
	  	}
		++count;
		
		if(!wcp->dispatch) {
			continue;
		}

		rc = wcp->dispatch(ch, wcp->udata);
		if(!rc) {
			g_source_remove_poll(source, &wcp->gpfd);
			g_source_unref(source);
			break;
		}
	}
	CHECK_DISPATCH_TIME(wcp);
	return rc;
}
Ejemplo n.º 14
0
static void
dx_dinput_finalize (GObject *object)
{
  ControllerDXDInput *controller = CONTROLLER_DX_DINPUT (object);

  if (controller->source != NULL)
    {
      g_source_remove_poll (controller->source, controller->pollfd);
      g_source_remove (g_source_get_id (controller->source));
      g_source_unref (controller->source);
    }

  if (controller->didevice8)
    IDirectInputDevice8_SetEventNotification (controller->didevice8, NULL);

  if (controller->format != NULL)
    {
      g_free (controller->format->rgodf);
      g_free (controller->format);
      controller->format = NULL;
    }

  g_free (controller->prevdata);
  controller->prevdata = NULL;

  if (controller->event != NULL)
    {
      CloseHandle (controller->event);
      controller->event = 0;
    }

  if (controller->store)
    {
      g_object_unref (controller->store);
      controller->store = NULL;
    }

  G_OBJECT_CLASS (controller_dx_dinput_parent_class)->finalize (object);
}
Ejemplo n.º 15
0
QEventDispatcherGlib::~QEventDispatcherGlib()
{
    Q_D(QEventDispatcherGlib);

    // destroy all timer sources
    qDeleteAll(d->timerSource->timerList);
    d->timerSource->timerList.~QTimerInfoList();
    g_source_destroy(&d->timerSource->source);
    g_source_unref(&d->timerSource->source);
    d->timerSource = 0;
#ifndef QT_WEBOS
    g_source_destroy(&d->idleTimerSource->source);
#endif // QT_WEBOS
    g_source_unref(&d->idleTimerSource->source);
    d->idleTimerSource = 0;

    // destroy socket notifier source
    for (int i = 0; i < d->socketNotifierSource->pollfds.count(); ++i) {
        GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds[i];
        g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd);
        delete p;
    }
    d->socketNotifierSource->pollfds.~QList<GPollFDWithQSocketNotifier *>();
    g_source_destroy(&d->socketNotifierSource->source);
    g_source_unref(&d->socketNotifierSource->source);
    d->socketNotifierSource = 0;

    // destroy post event source
    g_source_destroy(&d->postEventSource->source);
    g_source_unref(&d->postEventSource->source);
    d->postEventSource = 0;

    Q_ASSERT(d->mainContext != 0);
#if GLIB_CHECK_VERSION (2, 22, 0)
    g_main_context_pop_thread_default (d->mainContext);
#endif
    g_main_context_unref(d->mainContext);
    d->mainContext = 0;
}
Ejemplo n.º 16
0
static void session_setup_g_source(Session *s) {

	if (!s->ggs) return;
	
	if (s->g_source && s->g_pollfd.fd != s->ggs->fd) {
		g_source_remove_poll(s->g_source, &s->g_pollfd);
	}

	if (!s->g_source) {
		s->g_source = g_source_new(&source_funcs, sizeof(Source));
		((Source *)(gpointer)s->g_source)->session=s;
		g_source_attach(s->g_source,g_main_loop_get_context(main_loop));
	}
	
	s->g_pollfd.events=G_IO_ERR|G_IO_HUP|G_IO_NVAL;
	if (s->ggs->check&GG_CHECK_READ) s->g_pollfd.events|=G_IO_IN;
	if (s->ggs->check&GG_CHECK_WRITE) s->g_pollfd.events|=G_IO_OUT;

	if (s->g_pollfd.fd != s->ggs->fd) {
		s->g_pollfd.fd=s->ggs->fd;
		if (s->g_pollfd.fd!=-1) g_source_add_poll(s->g_source, &s->g_pollfd);
	}
}
Ejemplo n.º 17
0
Archivo: main.c Proyecto: dsd/fprintd
static void pollfd_removed_cb(int fd)
{
	GSList *elem = fdsource->pollfds;
	g_message("no longer monitoring fd %d", fd);

	if (!elem) {
		g_warning("cannot remove from list as list is empty?");
		return;
	}

	do {
		GPollFD *pollfd = elem->data;
		if (pollfd->fd != fd)
			continue;

		g_source_remove_poll((GSource *) fdsource, pollfd);
		g_slice_free(GPollFD, pollfd);
		fdsource->pollfds = g_slist_delete_link(fdsource->pollfds, elem);
		return;
	} while ((elem = g_slist_next(elem)));
	
	g_error("couldn't find fd %d in list\n", fd);
}
Ejemplo n.º 18
0
static gboolean
port_check(GSource *source)
{
	PSource *pn = (PSource *)source;
    uint_t nget;
    
    if (pn->pending) {
        pn->pending = FALSE;
        g_source_add_poll(source, PGPFD(source));
        g_source_unref(source);
        return FALSE;
    }

    if (!(PGPFD(pn)->revents & G_IO_IN))
        return FALSE;

    if (port_getn(PGPFD(source)->fd, NULL, 0, &nget, 0) == 0) {
        if (nget - pn->pending_events > EXPECT_INC_EVENTS(pn)) {
            /* Sleep for a while. */
            pn->pending_events = nget;
            pn->event_growing_factor ++;

            pn->pending = TRUE;
            g_source_ref(source);
            g_source_remove_poll(source, PGPFD(source));
            g_timeout_add(SLEEP_BASE_TIME,
              (GSourceFunc)port_check,
              (gpointer)pn);
            return FALSE;
        }
    }

    pn->pending_events = 0;
    pn->event_growing_factor = 0;

    return TRUE;
}
Ejemplo n.º 19
0
static gboolean
hm_watch_rw_dispatch(HmWatch *watch, gpointer user_data)
{
    HmWatchFuncs *funcs;
    gint err = 0, timeout = 1;
    socklen_t len = sizeof(err);
    gchar buf[MAX_IO_BUFFER_SIZE];

    funcs = watch->funcs;
    BUG_ON(!funcs);

    if (watch->r_fd.revents & READ_COND)
    {
        timeout = 0;

        if (watch->r_fd.revents & READ_ERR)
        {
            getsockopt(watch->r_fd.fd, SOL_SOCKET, SO_ERROR, &err, &len);
            err = -err;
            goto read_error;
        }

        for (;;)
        {
            if (watch->killed)
                return FALSE;   /* end the loop if killed, funcs->recv() may drop lock. */

            err = hm_connection_read(watch->conn, buf, MAX_IO_BUFFER_SIZE);
            if (err == 0)
            {
                goto conn_reset;
            }
            else if (err > 0)
            {
                if (funcs->recv)
                {
                    if ((err = (*funcs->recv)(watch, buf, err)))
                    {
                        goto read_error;
                    }
                }
                else
                {
                    watch->r_fd.revents = 0;    /* no reader */
                    g_source_remove_poll((GSource*)watch, &watch->r_fd);
                    break;  /* data will stay in the sock buffer */
                }
            }
            else
            {
                if (err == -EAGAIN)
                    break;

                goto read_error;
            }
        }
    }

    if (watch->w_fd.revents & WRITE_COND)
    {
        timeout = 0;

        if (G_UNLIKELY(hm_connection_is_ingrogress(watch->conn, 1)))
        {
            if (getsockopt(watch->w_fd.fd, SOL_SOCKET, SO_ERROR, &err, &len))
            {
                hm_warning("getsockopt() after connect() failed");
                err = -errno;
                goto write_error;
            }

            if (err)
            {
                err = -err;
                goto write_error;
            }

            watch->w_fd.revents = 0;
            g_source_remove_poll((GSource*)watch, &watch->w_fd);
            g_source_add_poll((GSource*)watch, &watch->r_fd);

            g_mutex_unlock(watch->lock);    /* drop the lock*/
            hm_watch_on_establish(watch);
            g_mutex_lock(watch->lock);          
        }
        else
        {
            if (watch->w_fd.revents & WRITE_ERR)
            {
                len = sizeof(err);
                getsockopt(watch->w_fd.fd, SOL_SOCKET, SO_ERROR, &err, &len);
                err = -err;
                goto write_error;
            }
    
            err = hm_net_buf_flush(watch->buffer, watch);
            if (!err)   /* all data flushed */
            {
                watch->w_fd.revents = 0;
                g_source_remove_poll((GSource*)watch, &watch->w_fd);
                watch->w_pending = 0;
            }
            else
            {
                if (err < 0)
                    goto write_error;
            }
        }
    }

    if (timeout)
    {
        hm_print(
            "Net IO '%p' timeout.", NET_IO(watch)
        );
        __hm_watch_error(watch, 0, -E_CONNTIMEOUT);
        return FALSE;
    }

    return TRUE;

conn_reset:
    __hm_watch_close(watch, 1);
    return FALSE;

read_error:
    __hm_watch_error(watch, 0, err);
    return FALSE;

write_error:
    __hm_watch_error(watch, 1, err);
    return FALSE;       
}
Ejemplo n.º 20
0
void main_loop_test()
{
	GMainContext *default_context;
	int depth;
	int id;
	GTimeVal time ={0,};
	int user_data = 0,fd_data = 0;
	GPollFD pollfd;
	GSource *source3;

	GSourceFuncs SourceFuncs =
	{
		prepare,
		check,
		dispatch,
		NULL
	};

	GSourceFuncs fd_SourceFuncs =
	{
		fd_prepare,
		fd_check,
		fd_dispatch,
		NULL
	};

	e1_complete = FALSE;
	e2_complete = FALSE;

	pipe(fd);

	pollfd.fd = fd[0];
	pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
	pollfd.revents = 0;

	pthread_create(&thread1, NULL, thread_function, NULL);

	context = g_main_context_new();

	//g_main_context_add_poll(context,&pollfd,0);

	source1 = g_source_new(&SourceFuncs,sizeof(GSource));
	g_source_set_callback(source1,(GSourceFunc)my_callback,&user_data,NULL);

	id = g_source_attach(source1,context);

	g_assert(g_source_get_id(source1) == id);

	g_source_set_priority(source1,0);

	g_assert(g_source_get_priority(source1) == 0);

	loop = g_main_loop_new(context, FALSE);

	default_context = g_main_loop_get_context(loop);

	//checks g_main_loop_get_context
	g_assert(default_context == context);

	//checks g_main_loop_is_running
	g_assert(g_main_loop_is_running(loop) == FALSE);

	depth = g_main_depth();

	//checks g_main_depth
	g_assert(depth == 0);

	g_source_get_current_time(source1,&time);

	g_assert(time.tv_usec > 0);

	g_source_set_can_recurse(source1,TRUE);

	g_assert(g_source_get_can_recurse(source1) == TRUE);

	source2 = g_source_new(&fd_SourceFuncs,sizeof(GSource));
	g_source_set_callback(source2,(GSourceFunc)fd_callback,&fd_data,NULL);
	g_source_add_poll(source2,&pollfd);

	g_source_remove_poll(source2,&pollfd);

	// checks g_source_remove_poll
	g_assert(source2->poll_fds == NULL);

	g_source_add_poll(source2,&pollfd);

	// checks whether g_source_add_poll is successful.
	// one more check is done in fd_callback.
	// If that function is callled we are sure that add poll was successful
	g_assert(source2->poll_fds->data == &pollfd);

	source3 = g_source_ref(source2);

	g_assert(source3 == source2 && source2->ref_count == 2);

	g_source_unref(source3);

	id = g_source_attach(source2,context);

	//checks g_main_context_pending
	g_assert(g_main_context_pending(context));

	g_main_loop_run(loop);

	// ref is called here. Thats why two unrefs are called. If the 2nd unref is
	// callled with the unref, code should crash
	g_main_loop_ref(loop);

	g_main_loop_unref(loop);

	g_main_loop_unref(loop);

	//checks the number of times the call back function is called
	g_assert(user_data == 100);

	// checks whether set poll was successful and call back for the same
	// was called
	g_assert(fd_data == 1);
}
Ejemplo n.º 21
0
/*
 *	Some kind of event occurred - notify the user.
 */
gboolean
G_CH_dispatch_int(GSource * source,
	      GSourceFunc callback,
	      gpointer user_data)
{
	GCHSource* chp = (GCHSource*)source;
	longclock_t	dispstart;
	longclock_t	resume_start = zero_longclock;

	g_assert(IS_CHSOURCE(chp));
	CHECK_DISPATCH_DELAY(chp);


	if (chp->dontread){
		return TRUE;
	}

	/* Is output now unblocked? 
	 *
	 * If so, turn off OUTPUT_EVENTS to avoid going into
	 * a tight poll(2) loop.
	 */
	if (chp->fd_fdx) {
		if (chp->infd.revents & OUTPUT_EVENTS) {
			chp->infd.events &= ~OUTPUT_EVENTS;
		}
	}else if (chp->outfd.revents & OUTPUT_EVENTS) {
		chp->outfd.events &= ~OUTPUT_EVENTS;
	}
		
	if (ANYDEBUG) {
		resume_start = time_longclock();
	}

	chp->ch->ops->resume_io(chp->ch);

	if (ANYDEBUG) {
		longclock_t resume_end = time_longclock();
		unsigned long	ms;
		ms = longclockto_ms(sub_longclock(resume_end
		,	resume_start));
		if (ms > 10) {
			cl_log(LOG_WARNING
			,	"%s: resume_io() for %s took %lu ms"
			,	__FUNCTION__
			,	chp->description, ms);
		}
	}


	if(chp->dispatch && chp->ch->ops->is_message_pending(chp->ch)) {
		if(!(chp->dispatch(chp->ch, chp->udata))){
			g_source_remove_poll(source, &chp->infd);
			if (!chp->fd_fdx) {
				g_source_remove_poll(source, &chp->outfd);
			}
			CHECK_DISPATCH_TIME(chp);
			g_source_unref(source);
			return FALSE;
		}
	}
	CHECK_DISPATCH_TIME(chp);

	if (chp->ch->ch_status == IPC_DISCONNECT){
		return FALSE;
	}
	return TRUE;
}
Ejemplo n.º 22
0
/**
 * Updates all registered GPollFD objects, unregisters old ones,
 * registers new ones.
 */
static void
http_client_update_fds(void)
{
	fd_set rfds, wfds, efds;
	int max_fd;
	CURLMcode mcode;
	GSList *fds;

	FD_ZERO(&rfds);
	FD_ZERO(&wfds);
	FD_ZERO(&efds);

	mcode = curl_multi_fdset(http_client.multi, &rfds, &wfds,
				 &efds, &max_fd);
	if (mcode != CURLM_OK) {
		g_warning("curl_multi_fdset() failed: %s\n",
			  curl_multi_strerror(mcode));
		return;
	}

	fds = http_client.fds;
	http_client.fds = NULL;

	while (fds != NULL) {
		GPollFD *poll_fd = fds->data;
		gushort events = http_client_fd_events(poll_fd->fd, &rfds,
						       &wfds, &efds);

		assert(poll_fd->events != 0);

		fds = g_slist_remove(fds, poll_fd);

		if (events != poll_fd->events)
			g_source_remove_poll(http_client.source, poll_fd);

		if (events != 0) {
			if (events != poll_fd->events) {
				poll_fd->events = events;
				g_source_add_poll(http_client.source, poll_fd);
			}

			http_client.fds = g_slist_prepend(http_client.fds,
							  poll_fd);
		} else {
			g_free(poll_fd);
		}
	}

	for (int fd = 0; fd <= max_fd; ++fd) {
		gushort events = http_client_fd_events(fd, &rfds,
						       &wfds, &efds);
		if (events != 0) {
			GPollFD *poll_fd = g_new(GPollFD, 1);
			poll_fd->fd = fd;
			poll_fd->events = events;
			g_source_add_poll(http_client.source, poll_fd);
			http_client.fds = g_slist_prepend(http_client.fds,
							  poll_fd);
		}
	}
}
Ejemplo n.º 23
0
static CoglBool
cogl_glib_source_prepare (GSource *source, int *timeout)
{
  CoglGLibSource *cogl_source = (CoglGLibSource *) source;
  CoglPollFD *poll_fds;
  int n_poll_fds;
  int64_t cogl_timeout;
  int age;
  int i;

  age = cogl_poll_renderer_get_info (cogl_source->renderer,
                                     &poll_fds,
                                     &n_poll_fds,
                                     &cogl_timeout);

  /* We have to be careful not to call g_source_add/remove_poll unless
   * the FDs have changed because it will cause the main loop to
   * immediately wake up. If we call it every time the source is
   * prepared it will effectively never go idle. */
  if (age != cogl_source->poll_fds_age)
    {
      /* Remove any existing polls before adding the new ones */
      for (i = 0; i < cogl_source->poll_fds->len; i++)
        {
          GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
          g_source_remove_poll (source, poll_fd);
        }

      g_array_set_size (cogl_source->poll_fds, n_poll_fds);

      for (i = 0; i < n_poll_fds; i++)
        {
          GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
          poll_fd->fd = poll_fds[i].fd;
          g_source_add_poll (source, poll_fd);
        }
    }

  cogl_source->poll_fds_age = age;

  /* Update the events */
  for (i = 0; i < n_poll_fds; i++)
    {
      GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
      poll_fd->events = poll_fds[i].events;
      poll_fd->revents = 0;
    }

  if (cogl_timeout == -1)
    {
      *timeout = -1;
      cogl_source->expiration_time = -1;
    }
  else
    {
      /* Round up to ensure that we don't try again too early */
      *timeout = (cogl_timeout + 999) / 1000;
      cogl_source->expiration_time = (g_source_get_time (source) +
                                      cogl_timeout);
    }

  return *timeout == 0;
}
Ejemplo n.º 24
0
/*
 * Unregister a previously registered CtkEvent from its corresponding event
 * source. If the event source becomes empty (no CtkEvents attached to it), this
 * function also destroys the event source and its corresponding event handle.
 */
static void ctk_event_unregister_source(CtkEvent *ctk_event)
{
    CtrlTarget *ctrl_target = ctk_event->ctrl_target;
    NvCtrlEventHandle *event_handle = NvCtrlGetEventHandle(ctrl_target);
    CtkEventSource *event_source;
    CtkEventNode *event_node;

    if (!event_handle) {
        return;
    }

    /* Do we have an event source for this event handle? */
    event_source = find_event_source(event_handle);

    if (!event_source) {
        return;
    }


    /* Remove the ctk_event object from the source's list of event objects */

    event_node = event_source->ctk_events;
    if (event_node->ctk_event == ctk_event) {
        event_source->ctk_events = event_node->next;
    }
    else {
        CtkEventNode *prev = event_node;
        event_node = event_node->next;
        while (event_node) {
            if (event_node->ctk_event == ctk_event) {
                prev->next = event_node->next;
                break;
            }
            prev = event_node;
            event_node = event_node->next;
        }
    }

    if (!event_node) {
        return;
    }

    g_free(event_node);


    /* destroy the event source if empty */

    if (event_source->ctk_events == NULL) {
        GSource *source = (GSource *)event_source;

        if (event_sources == event_source) {
            event_sources = event_source->next;
        }
        else {
            CtkEventSource *cur;
            for (cur = event_sources; cur; cur = cur->next) {
                if (cur->next == event_source) {
                    cur->next = event_source->next;
                    break;
                }
            }
        }

        NvCtrlCloseEventHandle(event_source->event_handle);
        g_source_remove_poll(source, &(event_source->event_poll_fd));
        g_source_destroy(source);
        g_source_unref(source);
    }
}
Ejemplo n.º 25
0
XlibEventSource::~XlibEventSource()
{
    g_source_remove_poll(&m_source->source, &m_pollFD);
    g_source_destroy(&m_source->source);
}
Ejemplo n.º 26
0
static void g_source_fd_finalize (GSource *source)
{
  GSourceFD *sfd = (GSourceFD *) source;
  if (sfd->has_poll)
    g_source_remove_poll (source, &sfd->poll_fd);
}
Ejemplo n.º 27
0
/** @internal
 *
 *  Register a @c su_wait_t object. The wait object, a callback function and
 *  a argument pointer is stored in the port object.  The callback function
 *  will be called when the wait object is signaled.
 *
 *  Please note if identical wait objects are inserted, only first one is
 *  ever signalled.
 *
 * @param self	     pointer to port
 * @param root	     pointer to root object
 * @param waits	     pointer to wait object
 * @param callback   callback function pointer
 * @param arg	     argument given to callback function when it is invoked
 * @param priority   relative priority of the wait object
 *              (0 is normal, 1 important, 2 realtime)
 *
 * @return
 *   The function @su_source_register returns nonzero index of the wait object,
 *   or -1 upon an error.  */
int su_source_register(su_port_t *self,
		       su_root_t *root,
		       su_wait_t *wait,
		       su_wakeup_f callback,
		       su_wakeup_arg_t *arg,
		       int priority)
{
  unsigned i, j, I;
  unsigned n;

  enter;

  assert(SU_SOURCE_OWN_THREAD(self));

  n = self->sup_n_waits;

  if (n >= self->sup_size_waits) {
    /* Reallocate size arrays */
    unsigned size;
    unsigned *indices;
    su_wait_t *waits;
    su_wakeup_f *wait_cbs;
    su_wakeup_arg_t **wait_args;
    su_root_t **wait_tasks;

    if (self->sup_size_waits == 0)
      size = SU_WAIT_MIN;
    else
      size = 2 * self->sup_size_waits;

    indices = realloc(self->sup_indices, size * sizeof(*indices));
    if (indices) {
      self->sup_indices = indices;

      for (i = self->sup_size_waits; i < size; i++)
	indices[i] = UINT_MAX;
    }

    for (i = 0; i < self->sup_n_waits; i++)
      g_source_remove_poll(self->sup_source, (GPollFD*)&self->sup_waits[i]);

    waits = realloc(self->sup_waits, size * sizeof(*waits));
    if (waits)
      self->sup_waits = waits;

    for (i = 0; i < self->sup_n_waits; i++)
      g_source_add_poll(self->sup_source, (GPollFD*)&waits[i]);

    wait_cbs = realloc(self->sup_wait_cbs, size * sizeof(*wait_cbs));
    if (wait_cbs)
      self->sup_wait_cbs = wait_cbs;

    wait_args = realloc(self->sup_wait_args, size * sizeof(*wait_args));
    if (wait_args)
      self->sup_wait_args = wait_args;

    /* Add sup_wait_roots array, if needed */
    wait_tasks = realloc(self->sup_wait_roots, size * sizeof(*wait_tasks));
    if (wait_tasks)
      self->sup_wait_roots = wait_tasks;

    if (!(indices && waits && wait_cbs && wait_args && wait_tasks)) {
      return -1;
    }

    self->sup_size_waits = size;
  }

  self->sup_n_waits++;

  if (priority > 0) {
    /* Insert */
    for (; n > 0; n--) {
      g_source_remove_poll(self->sup_source, (GPollFD*)&self->sup_waits[n-1]);
      self->sup_waits[n] = self->sup_waits[n-1];
      g_source_add_poll(self->sup_source, (GPollFD*)&self->sup_waits[n]);
      self->sup_wait_cbs[n] = self->sup_wait_cbs[n-1];
      self->sup_wait_args[n] = self->sup_wait_args[n-1];
      self->sup_wait_roots[n] = self->sup_wait_roots[n-1];
    }
  }
  else {
    /* Append - no need to move anything */
  }

  self->sup_waits[n] = *wait;
  g_source_add_poll(self->sup_source, (GPollFD*)&self->sup_waits[n]);
  self->sup_wait_cbs[n] = callback;
  self->sup_wait_args[n] = arg;
  self->sup_wait_roots[n] = root;

  I = self->sup_max_index;

  for (i = 0; i < I; i++)
    if (self->sup_indices[i] == UINT_MAX)
      break;
    else if (self->sup_indices[i] >= n)
      self->sup_indices[i]++;

  if (i == I)
    self->sup_max_index++;

  if (n + 1 < self->sup_n_waits)
    for (j = i; j < I; j++)
      if (self->sup_indices[j] != UINT_MAX &&
	  self->sup_indices[j] >= n)
	self->sup_indices[j]++;

  self->sup_indices[i] = n;

  self->sup_registers++;

  return i + 1;			/* 0 is failure */
}
Ejemplo n.º 28
0
/** Unregister a su_wait_t object.
 *
 *  The function su_source_unregister() unregisters a su_wait_t object. The
 *  wait object, a callback function and a argument are removed from the
 *  port object.
 *
 * @param self     - pointer to port object
 * @param root     - pointer to root object
 * @param wait     - pointer to wait object
 * @param callback - callback function pointer (may be NULL)
 * @param arg      - argument given to callback function when it is invoked
 *                   (may be NULL)
 *
 * @return Nonzero index of the wait object, or -1 upon an error.
 */
int su_source_unregister(su_port_t *self,
			 su_root_t *root,
			 su_wait_t *wait,
			 su_wakeup_f callback, /* XXX - ignored */
			 su_wakeup_arg_t *arg)
{
  unsigned n, N;
  unsigned i, I, j, *indices;

  enter;

  assert(self);
  assert(SU_SOURCE_OWN_THREAD(self));

  i = (unsigned)-1;
  N = self->sup_n_waits;
  I = self->sup_max_index;
  indices = self->sup_indices;

  for (n = 0; n < N; n++) {
    if (SU_WAIT_CMP(wait[0], self->sup_waits[n]) != 0)
      continue;

    /* Found - delete it */
    if (indices[n] == n)
      i = n;
    else for (i = 0; i < I; i++)
      if (indices[i] == n)
	break;

    assert(i < I);

    indices[i] = UINT_MAX;

    g_source_remove_poll(self->sup_source, (GPollFD*)&self->sup_waits[n]);

    self->sup_n_waits = N = N - 1;

    if (n < N)
      for (j = 0; j < I; j++)
	if (self->sup_indices[j] != UINT_MAX &&
	    self->sup_indices[j] > n)
	  self->sup_indices[j]--;

    for (; n < N; n++) {
      g_source_remove_poll(self->sup_source, (GPollFD*)&self->sup_waits[n+1]);
      self->sup_waits[n] = self->sup_waits[n+1];
      g_source_add_poll(self->sup_source, (GPollFD*)&self->sup_waits[n]);
      self->sup_wait_cbs[n] = self->sup_wait_cbs[n+1];
      self->sup_wait_args[n] = self->sup_wait_args[n+1];
      self->sup_wait_roots[n] = self->sup_wait_roots[n+1];
    }

    i += 1;	/* 0 is failure */

    if (i == I)
      self->sup_max_index--;

    break;
  }

  self->sup_registers++;

  return (int)i;
}