ObjFW  Diff

Differences From Artifact [a843dfe040]:

To Artifact [392955f6f6]:


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
72
73
74
75
76
77
78






79
80
81
82
83
84
85







-
-
-
-
-
-







# ifdef OF_HAVE_THREADS
	OFMutex *_execSignalsMutex;
# endif
#endif
}
@end

OF_DIRECT_MEMBERS
@interface OFRunLoop ()
- (OFRunLoopState *)of_stateForMode: (of_run_loop_mode_t)mode
			     create: (bool)create;
@end

#ifdef OF_HAVE_SOCKETS
@interface OFRunLoopQueueItem: OFObject
{
@public
	id _delegate;
}

425
426
427
428
429
430
431
432

433
434
435
436
437
438
439
440
419
420
421
422
423
424
425

426

427
428
429
430
431
432
433







-
+
-







@implementation OFRunLoopReadQueueItem
- (bool)handleObject: (id)object
{
	size_t length;
	id exception = nil;

	@try {
		length = [object readIntoBuffer: _buffer
		length = [object readIntoBuffer: _buffer length: _length];
					 length: _length];
	} @catch (id e) {
		length = 0;
		exception = e;
	}

# ifdef OF_HAVE_BLOCKS
	if (_block != NULL)
734
735
736
737
738
739
740
741
742

743
744
745
746
747
748
749
750
727
728
729
730
731
732
733


734

735
736
737
738
739
740
741







-
-
+
-







		    timerWithTimeInterval: 0
				   target: _delegate
				 selector: @selector(of_socketDidConnect:
					       exception:)
				   object: object
				   object: exception
				  repeats: false];

		[runLoop addTimer: timer
		[runLoop addTimer: timer forMode: runLoop.currentMode];
			  forMode: runLoop.currentMode];
	}

	return false;
}
@end
# endif

905
906
907
908
909
910
911
912

913
914
915
916
917
918
919
920
896
897
898
899
900
901
902

903

904
905
906
907
908
909
910







-
+
-







@implementation OFRunLoopPacketReceiveQueueItem
- (bool)handleObject: (id)object
{
	size_t length;
	id exception = nil;

	@try {
		length = [object receiveIntoBuffer: _buffer
		length = [object receiveIntoBuffer: _buffer length: _length];
					    length: _length];
	} @catch (id e) {
		length = 0;
		exception = e;
	}

# ifdef OF_HAVE_BLOCKS
	if (_block != NULL)
1026
1027
1028
1029
1030
1031
1032
1033

1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051

1052
1053
1054
1055
1056
1057
1058
1059
1016
1017
1018
1019
1020
1021
1022

1023

1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039

1040

1041
1042
1043
1044
1045
1046
1047







-
+
-
















-
+
-







	mainRunLoop = [runLoop retain];
}

#ifdef OF_HAVE_SOCKETS
# define NEW_READ(type, object, mode)					\
	void *pool = objc_autoreleasePoolPush();			\
	OFRunLoop *runLoop = [self currentRunLoop];			\
	OFRunLoopState *state = [runLoop of_stateForMode: mode		\
	OFRunLoopState *state = stateForMode(runLoop, mode, true);	\
						  create: true];	\
	OFList *queue = [state->_readQueues objectForKey: object];	\
	type *queueItem;						\
									\
	if (queue == nil) {						\
		queue = [OFList list];					\
		[state->_readQueues setObject: queue forKey: object];	\
	}								\
									\
	if (queue.count == 0)						\
		[state->_kernelEventObserver				\
		    addObjectForReading: object];			\
									\
	queueItem = [[[type alloc] init] autorelease];
# define NEW_WRITE(type, object, mode)					\
	void *pool = objc_autoreleasePoolPush();			\
	OFRunLoop *runLoop = [self currentRunLoop];			\
	OFRunLoopState *state = [runLoop of_stateForMode: mode		\
	OFRunLoopState *state = stateForMode(runLoop, mode, true);	\
						  create: true];	\
	OFList *queue = [state->_writeQueues objectForKey: object];	\
	type *queueItem;						\
									\
	if (queue == nil) {						\
		queue = [OFList list];					\
		[state->_writeQueues setObject: queue forKey: object];	\
	}								\
1289
1290
1291
1292
1293
1294
1295
1296

1297
1298
1299
1300
1301
1302
1303
1304
1277
1278
1279
1280
1281
1282
1283

1284

1285
1286
1287
1288
1289
1290
1291







-
+
-







# undef QUEUE_ITEM

+ (void)of_cancelAsyncRequestsForObject: (id)object
				   mode: (of_run_loop_mode_t)mode
{
	void *pool = objc_autoreleasePoolPush();
	OFRunLoop *runLoop = [self currentRunLoop];
	OFRunLoopState *state = [runLoop of_stateForMode: mode
	OFRunLoopState *state = stateForMode(runLoop, mode, false);
						  create: false];
	OFList *queue;

	if (state == nil)
		return;

	if ((queue = [state->_writeQueues objectForKey: object]) != nil) {
		assert(queue.count > 0);
1364
1365
1366
1367
1368
1369
1370
1371
1372


1373
1374
1375
1376
1377

1378
1379
1380

1381
1382
1383
1384
1385

1386
1387
1388
1389
1390
1391
1392

1393
1394
1395
1396
1397
1398
1399
1400
1401

1402
1403
1404
1405

1406
1407
1408

1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422

1423
1424
1425
1426
1427
1428
1429
1430
1431
1432

1433
1434
1435

1436
1437
1438
1439
1440
1441
1442
1443
1351
1352
1353
1354
1355
1356
1357


1358
1359
1360
1361
1362
1363

1364
1365
1366

1367
1368
1369
1370
1371

1372
1373
1374
1375
1376
1377
1378

1379
1380
1381
1382
1383
1384
1385
1386
1387

1388

1389
1390

1391

1392

1393

1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405

1406

1407
1408
1409
1410
1411
1412
1413
1414

1415

1416

1417

1418
1419
1420
1421
1422
1423
1424







-
-
+
+




-
+


-
+




-
+






-
+








-
+
-


-
+
-

-
+
-












-
+
-








-
+
-

-
+
-







#ifdef OF_HAVE_THREADS
	[_statesMutex release];
#endif

	[super dealloc];
}

- (OFRunLoopState *)of_stateForMode: (of_run_loop_mode_t)mode
			     create: (bool)create
static OFRunLoopState *
stateForMode(OFRunLoop *self, of_run_loop_mode_t mode, bool create)
{
	OFRunLoopState *state;

#ifdef OF_HAVE_THREADS
	[_statesMutex lock];
	[self->_statesMutex lock];
	@try {
#endif
		state = [_states objectForKey: mode];
		state = [self->_states objectForKey: mode];

		if (create && state == nil) {
			state = [[OFRunLoopState alloc] init];
			@try {
				[_states setObject: state forKey: mode];
				[self->_states setObject: state forKey: mode];
			} @finally {
				[state release];
			}
		}
#ifdef OF_HAVE_THREADS
	} @finally {
		[_statesMutex unlock];
		[self->_statesMutex unlock];
	}
#endif

	return state;
}

- (void)addTimer: (OFTimer *)timer
{
	[self addTimer: timer
	[self addTimer: timer forMode: of_run_loop_mode_default];
	       forMode: of_run_loop_mode_default];
}

- (void)addTimer: (OFTimer *)timer
- (void)addTimer: (OFTimer *)timer forMode: (of_run_loop_mode_t)mode
	 forMode: (of_run_loop_mode_t)mode
{
	OFRunLoopState *state = [self of_stateForMode: mode
	OFRunLoopState *state = stateForMode(self, mode, true);
					       create: true];

#ifdef OF_HAVE_THREADS
	[state->_timersQueueMutex lock];
	@try {
#endif
		[state->_timersQueue insertObject: timer];
#ifdef OF_HAVE_THREADS
	} @finally {
		[state->_timersQueueMutex unlock];
	}
#endif

	[timer of_setInRunLoop: self
	[timer of_setInRunLoop: self mode: mode];
			  mode: mode];

#if defined(OF_HAVE_SOCKETS)
	[state->_kernelEventObserver cancel];
#elif defined(OF_HAVE_THREADS)
	[state->_condition signal];
#endif
}

- (void)of_removeTimer: (OFTimer *)timer
- (void)of_removeTimer: (OFTimer *)timer forMode: (of_run_loop_mode_t)mode
	       forMode: (of_run_loop_mode_t)mode
{
	OFRunLoopState *state = [self of_stateForMode: mode
	OFRunLoopState *state = stateForMode(self, mode, false);
					       create: false];

	if (state == nil)
		return;

#ifdef OF_HAVE_THREADS
	[state->_timersQueueMutex lock];
	@try {
1455
1456
1457
1458
1459
1460
1461
1462

1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477

1478
1479
1480
1481
1482
1483
1484
1485
1436
1437
1438
1439
1440
1441
1442

1443


1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455

1456

1457
1458
1459
1460
1461
1462
1463







-
+
-
-












-
+
-







	} @finally {
		[state->_timersQueueMutex unlock];
	}
#endif
}

#ifdef OF_AMIGAOS
- (void)addExecSignal: (ULONG)signal
- (void)addExecSignal: (ULONG)signal target: (id)target selector: (SEL)selector
	       target: (id)target
	     selector: (SEL)selector
{
	[self addExecSignal: signal
		    forMode: of_run_loop_mode_default
		     target: target
		   selector: selector];
}

- (void)addExecSignal: (ULONG)signal
	      forMode: (of_run_loop_mode_t)mode
	       target: (id)target
	     selector: (SEL)selector
{
	OFRunLoopState *state = [self of_stateForMode: mode
	OFRunLoopState *state = stateForMode(self, mode, true);
					       create: true];

# ifdef OF_HAVE_THREADS
	[state->_execSignalsMutex lock];
	@try {
# endif
		[state->_execSignals addItem: &signal];
		[state->_execSignalsTargets addObject: target];
1514
1515
1516
1517
1518
1519
1520
1521

1522
1523
1524
1525
1526
1527
1528
1529
1492
1493
1494
1495
1496
1497
1498

1499

1500
1501
1502
1503
1504
1505
1506







-
+
-







}

- (void)removeExecSignal: (ULONG)signal
		 forMode: (of_run_loop_mode_t)mode
		  target: (id)target
		selector: (SEL)selector
{
	OFRunLoopState *state = [self of_stateForMode: mode
	OFRunLoopState *state = stateForMode(self, mode, false);
					       create: false];

	if (state == nil)
		return;

# ifdef OF_HAVE_THREADS
	[state->_execSignalsMutex lock];
	@try {
1575
1576
1577
1578
1579
1580
1581
1582

1583
1584
1585
1586

1587
1588
1589
1590
1591

1592
1593
1594
1595
1596
1597
1598
1599
1552
1553
1554
1555
1556
1557
1558

1559

1560
1561

1562

1563
1564
1565

1566

1567
1568
1569
1570
1571
1572
1573







-
+
-


-
+
-



-
+
-








- (void)runUntilDate: (OFDate *)deadline
{
	_stop = false;

	while (!_stop &&
	    (deadline == nil || deadline.timeIntervalSinceNow >= 0))
		[self runMode: of_run_loop_mode_default
		[self runMode: of_run_loop_mode_default beforeDate: deadline];
		   beforeDate: deadline];
}

- (void)runMode: (of_run_loop_mode_t)mode
- (void)runMode: (of_run_loop_mode_t)mode beforeDate: (OFDate *)deadline
     beforeDate: (OFDate *)deadline
{
	void *pool = objc_autoreleasePoolPush();
	of_run_loop_mode_t previousMode = _currentMode;
	OFRunLoopState *state = [self of_stateForMode: mode
	OFRunLoopState *state = stateForMode(self, mode, false);
					       create: false];

	if (state == nil)
		return;

	_currentMode = mode;
	@try {
		OFDate *nextTimer;
1615
1616
1617
1618
1619
1620
1621
1622

1623
1624
1625
1626
1627
1628
1629
1630
1589
1590
1591
1592
1593
1594
1595

1596

1597
1598
1599
1600
1601
1602
1603







-
+
-







				    fireDate].timeIntervalSinceNow <= 0) {
					timer = [[listObject->object
					    retain] autorelease];

					[state->_timersQueue
					    removeListObject: listObject];

					[timer of_setInRunLoop: nil
					[timer of_setInRunLoop: nil mode: nil];
							  mode: nil];
				} else
					break;
#ifdef OF_HAVE_THREADS
			} @finally {
				[state->_timersQueueMutex unlock];
			}
#endif
1719
1720
1721
1722
1723
1724
1725
1726
1727


1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1692
1693
1694
1695
1696
1697
1698


1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713







-
-
+
+













	} @finally {
		_currentMode = previousMode;
	}
}

- (void)stop
{
	OFRunLoopState *state = [self of_stateForMode: of_run_loop_mode_default
					       create: false];
	OFRunLoopState *state =
	    stateForMode(self, of_run_loop_mode_default, false);

	_stop = true;

	if (state == nil)
		return;

#if defined(OF_HAVE_SOCKETS)
	[state->_kernelEventObserver cancel];
#elif defined(OF_HAVE_THREADS)
	[state->_condition signal];
#endif
}
@end