~ubuntu-branches/ubuntu/utopic/tinymux/utopic

« back to all changes in this revision

Viewing changes to src/cque.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ervin Hearn III
  • Date: 2008-04-11 23:18:25 UTC
  • mfrom: (1.1.5 upstream) (6.1.1 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080411231825-1pq4trckagyk8roo
Tags: 2.6.5.27-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// cque.cpp -- commands and functions for manipulating the command queue.
2
 
//
3
 
// $Id: cque.cpp,v 1.35 2006/01/07 23:46:32 sdennis Exp $
4
 
//
5
 
// MUX 2.4
6
 
// Copyright (C) 1998 through 2004 Solid Vertical Domains, Ltd. All
7
 
// rights not explicitly given are reserved.
8
 
//
 
1
/*! \file cque.cpp
 
2
 *  Commands and functions for manipulating the command queue.
 
3
 *
 
4
 * $Id: cque.cpp 2803 2007-11-27 00:30:39Z brazilofmux $
 
5
 *
 
6
 * This forms the upper-level command list queue, and includes timed commands
 
7
 * and semaphores.  The lower-level task implementation is found in timer.cpp.
 
8
 */
 
9
 
9
10
#include "copyright.h"
10
11
#include "autoconf.h"
11
12
#include "config.h"
24
25
{
25
26
    CLinearTimeDelta ltd;
26
27
#ifdef WIN32
27
 
    if (platform == VER_PLATFORM_WIN32_NT)
 
28
    if (fpGetProcessTimes)
28
29
    {
29
30
        FILETIME ftCreate;
30
31
        FILETIME ftExit;
70
71
    num += am;
71
72
 
72
73
    char buff[20];
73
 
    int nlen = 0;
 
74
    size_t nlen = 0;
74
75
    *buff = '\0';
75
76
    if (num)
76
77
    {
104
105
            //
105
106
            for (int i = 0; i < MAX_GLOBAL_REGS; i++)
106
107
            {
107
 
                if (point->scr[i])
108
 
                {
109
 
                    int n = strlen(point->scr[i]);
110
 
                    memcpy(mudstate.global_regs[i], point->scr[i], n+1);
111
 
                    mudstate.glob_reg_len[i] = n;
112
 
                }
113
 
                else
114
 
                {
115
 
                    mudstate.global_regs[i][0] = '\0';
116
 
                    mudstate.glob_reg_len[i] = 0;
117
 
                }
 
108
                if (mudstate.global_regs[i])
 
109
                {
 
110
                    RegRelease(mudstate.global_regs[i]);
 
111
                    mudstate.global_regs[i] = NULL;
 
112
                }
 
113
                mudstate.global_regs[i] = point->scr[i];
 
114
                point->scr[i] = NULL;
118
115
            }
119
116
 
120
117
            char *command = point->comm;
162
159
                    CLinearTimeDelta ltdUsageBegin = GetProcessorUsage();
163
160
 
164
161
                    char *log_cmdbuf = process_command(executor, point->caller,
165
 
                        point->enactor, false, cp, point->env, point->nargs);
 
162
                        point->enactor, point->eval, false, cp, point->env,
 
163
                        point->nargs);
166
164
 
167
165
                    CLinearTimeAbsolute ltaEnd;
168
166
                    ltaEnd.GetUTC();
186
184
                        STARTLOG(LOG_PROBLEMS, "CMD", "CPU");
187
185
                        log_name_and_loc(executor);
188
186
                        char *logbuf = alloc_lbuf("do_top.LOG.cpu");
189
 
                        sprintf(logbuf, " queued command taking %s secs (enactor #%d): ",
 
187
                        mux_sprintf(logbuf, LBUF_SIZE, " queued command taking %s secs (enactor #%d): ",
190
188
                            ltd.ReturnSecondsString(4), point->enactor);
191
189
                        log_text(logbuf);
192
190
                        free_lbuf(logbuf);
222
220
            mudstate.inpipe = false;
223
221
            mudstate.poutobj = NOTHING;
224
222
        }
225
 
        MEMFREE(point->text);
226
 
        point->text = NULL;
227
 
        free_qentry(point);
228
223
    }
229
224
 
230
225
    for (int i = 0; i < MAX_GLOBAL_REGS; i++)
231
226
    {
232
 
        mudstate.global_regs[i][0] = '\0';
233
 
        mudstate.glob_reg_len[i] = 0;
 
227
        if (point->scr[i])
 
228
        {
 
229
            RegRelease(point->scr[i]);
 
230
            point->scr[i] = NULL;
 
231
        }
 
232
 
 
233
        if (mudstate.global_regs[i])
 
234
        {
 
235
            RegRelease(mudstate.global_regs[i]);
 
236
            mudstate.global_regs[i] = NULL;
 
237
        }
234
238
    }
 
239
 
 
240
    MEMFREE(point->text);
 
241
    point->text = NULL;
 
242
    free_qentry(point);
235
243
}
236
244
 
237
245
// ---------------------------------------------------------------------------
312
320
            {
313
321
                add_to(point->sem, -1, point->attr);
314
322
            }
 
323
 
 
324
            for (int i = 0; i < MAX_GLOBAL_REGS; i++)
 
325
            {
 
326
                if (point->scr[i])
 
327
                {
 
328
                    RegRelease(point->scr[i]);
 
329
                    point->scr[i] = NULL;
 
330
                }
 
331
            }
 
332
 
315
333
            MEMFREE(point->text);
316
334
            point->text = NULL;
317
335
            free_qentry(point);
355
373
// ---------------------------------------------------------------------------
356
374
// do_halt: Command interface to halt_que.
357
375
//
358
 
void do_halt(dbref executor, dbref caller, dbref enactor, int key, char *target)
 
376
void do_halt(dbref executor, dbref caller, dbref enactor, int eval, int key, char *target)
359
377
{
360
378
    UNUSED_PARAMETER(caller);
361
379
    UNUSED_PARAMETER(enactor);
 
380
    UNUSED_PARAMETER(eval);
362
381
 
363
382
    dbref executor_targ, obj_targ;
364
383
 
450
469
                //
451
470
                giveto(point->executor, mudconf.waitcost);
452
471
                a_Queue(Owner(point->executor), -1);
 
472
 
 
473
                for (int i = 0; i < MAX_GLOBAL_REGS; i++)
 
474
                {
 
475
                    if (point->scr[i])
 
476
                    {
 
477
                        RegRelease(point->scr[i]);
 
478
                        point->scr[i] = NULL;
 
479
                    }
 
480
                }
 
481
 
453
482
                MEMFREE(point->text);
454
483
                point->text = NULL;
455
484
                free_qentry(point);
 
485
 
456
486
                return IU_REMOVE_TASK;
457
487
            }
458
488
            else
483
513
{
484
514
    // If we've notified enough, exit.
485
515
    //
486
 
    if (  Notify_Key == NFY_NFY
 
516
    if (  (  NFY_NFY == Notify_Key
 
517
          || NFY_QUIET == Notify_Key)
487
518
       && Notify_Num_Done >= Notify_Num_Max)
488
519
    {
489
520
        return IU_DONE;
554
585
 
555
586
    // Update the sem waiters count.
556
587
    //
557
 
    if (key == NFY_NFY)
 
588
    if (  NFY_NFY == key
 
589
       || NFY_QUIET == key)
558
590
    {
559
591
        add_to(sem, -count, attr);
560
592
    }
653
685
// ---------------------------------------------------------------------------
654
686
// setup_que: Set up a queue entry.
655
687
//
656
 
static BQUE *setup_que(dbref executor, dbref caller, dbref enactor,
657
 
                       char *command, char *args[], int nargs, char *sargs[])
 
688
static BQUE *setup_que
 
689
(
 
690
    dbref    executor,
 
691
    dbref    caller,
 
692
    dbref    enactor,
 
693
    int      eval,
 
694
    char    *command,
 
695
    int      nargs,
 
696
    char    *args[],
 
697
    reg_ref *sargs[]
 
698
)
658
699
{
659
 
    int a;
660
 
    BQUE *tmp;
661
 
 
662
700
    // Can we run commands at all?
663
701
    //
664
702
    if (Halted(executor))
 
703
    {
665
704
        return NULL;
 
705
    }
666
706
 
667
707
    // Make sure executor can afford to do it.
668
708
    //
669
 
    a = mudconf.waitcost;
 
709
    int a = mudconf.waitcost;
670
710
    if (mudconf.machinecost && RandomINT32(0, mudconf.machinecost-1) == 0)
671
711
    {
672
712
        a++;
700
740
 
701
741
    // Calculate the length of the save string.
702
742
    //
703
 
    unsigned int tlen = 0;
704
 
    static unsigned int nCommand;
705
 
    static unsigned int nLenEnv[NUM_ENV_VARS];
706
 
    static unsigned int nLenRegs[MAX_GLOBAL_REGS];
 
743
    size_t tlen = 0;
 
744
    static size_t nCommand;
 
745
    static size_t nLenEnv[NUM_ENV_VARS];
707
746
 
708
747
    if (command)
709
748
    {
710
749
        nCommand = strlen(command) + 1;
711
750
        tlen = nCommand;
712
751
    }
 
752
 
713
753
    if (nargs > NUM_ENV_VARS)
714
754
    {
715
755
        nargs = NUM_ENV_VARS;
716
756
    }
 
757
 
717
758
    for (a = 0; a < nargs; a++)
718
759
    {
719
760
        if (args[a])
722
763
            tlen += nLenEnv[a];
723
764
        }
724
765
    }
725
 
    if (sargs)
726
 
    {
727
 
        for (a = 0; a < MAX_GLOBAL_REGS; a++)
728
 
        {
729
 
            if (sargs[a])
730
 
            {
731
 
                nLenRegs[a] = strlen(sargs[a]) + 1;
732
 
                tlen += nLenRegs[a];
733
 
            }
734
 
        }
735
 
    }
736
766
 
737
767
    // Create the qeue entry and load the save string.
738
768
    //
739
 
    tmp = alloc_qentry("setup_que.qblock");
 
769
    BQUE *tmp = alloc_qentry("setup_que.qblock");
740
770
    tmp->comm = NULL;
741
771
 
742
772
    char *tptr = tmp->text = (char *)MEMALLOC(tlen);
748
778
        tmp->comm = tptr;
749
779
        tptr += nCommand;
750
780
    }
 
781
 
751
782
    for (a = 0; a < nargs; a++)
752
783
    {
753
784
        if (args[a])
761
792
            tmp->env[a] = NULL;
762
793
        }
763
794
    }
 
795
 
764
796
    for ( ; a < NUM_ENV_VARS; a++)
765
797
    {
766
798
        tmp->env[a] = NULL;
767
799
    }
768
 
    for (a = 0; a < MAX_GLOBAL_REGS; a++)
769
 
    {
770
 
        tmp->scr[a] = NULL;
771
 
    }
 
800
 
772
801
    if (sargs)
773
802
    {
774
803
        for (a = 0; a < MAX_GLOBAL_REGS; a++)
775
804
        {
 
805
            tmp->scr[a] = sargs[a];
776
806
            if (sargs[a])
777
807
            {
778
 
                memcpy(tptr, sargs[a], nLenRegs[a]);
779
 
                tmp->scr[a] = tptr;
780
 
                tptr += nLenRegs[a];
 
808
                RegAddRef(sargs[a]);
781
809
            }
782
810
        }
783
811
    }
 
812
    else
 
813
    {
 
814
        for (a = 0; a < MAX_GLOBAL_REGS; a++)
 
815
        {
 
816
            tmp->scr[a] = NULL;
 
817
        }
 
818
    }
784
819
 
785
820
    // Load the rest of the queue block.
786
821
    //
790
825
    tmp->attr = 0;
791
826
    tmp->enactor = enactor;
792
827
    tmp->caller = caller;
 
828
    tmp->eval = eval;
793
829
    tmp->nargs = nargs;
794
830
    return tmp;
795
831
}
799
835
//
800
836
void wait_que
801
837
(
802
 
    dbref executor,
803
 
    dbref caller,
804
 
    dbref enactor,
805
 
    bool bTimed,
 
838
    dbref    executor,
 
839
    dbref    caller,
 
840
    dbref    enactor,
 
841
    int      eval,
 
842
    bool     bTimed,
806
843
    CLinearTimeAbsolute &ltaWhen,
807
 
    dbref sem,
808
 
    int   attr,
809
 
    char *command,
810
 
    char *args[],
811
 
    int   nargs,
812
 
    char *sargs[]
 
844
    dbref    sem,
 
845
    int      attr,
 
846
    char    *command,
 
847
    int      nargs,
 
848
    char    *args[],
 
849
    reg_ref *sargs[]
813
850
)
814
851
{
815
852
    if (!(mudconf.control_flags & CF_INTERP))
817
854
        return;
818
855
    }
819
856
 
820
 
    BQUE *tmp = setup_que(executor, caller, enactor, command, args, nargs, sargs);
 
857
    BQUE *tmp = setup_que(executor, caller, enactor, eval,
 
858
        command,
 
859
        nargs, args,
 
860
        sargs);
 
861
 
821
862
    if (!tmp)
822
863
    {
823
864
        return;
872
913
//
873
914
void sql_que
874
915
(
875
 
    dbref executor,
876
 
    dbref caller,
877
 
    dbref enactor,
878
 
    bool bTimed,
 
916
    dbref    executor,
 
917
    dbref    caller,
 
918
    dbref    enactor,
 
919
    int      eval,
 
920
    bool     bTimed,
879
921
    CLinearTimeAbsolute &ltaWhen,
880
 
    dbref thing,
881
 
    int   attr,
882
 
    char *command,
883
 
    char *args[],
884
 
    int   nargs,
885
 
    char *sargs[]
 
922
    dbref    thing,
 
923
    int      attr,
 
924
    char    *command,
 
925
    int      nargs,
 
926
    char    *args[],
 
927
    reg_ref *sargs[]
886
928
)
887
929
{
888
930
    if (!(mudconf.control_flags & CF_INTERP))
890
932
        return;
891
933
    }
892
934
 
893
 
    BQUE *tmp = setup_que(executor, caller, enactor, command, args, nargs, sargs);
 
935
    BQUE *tmp = setup_que(executor, caller, enactor, eval,
 
936
        command,
 
937
        nargs, args,
 
938
        sargs);
 
939
 
894
940
    if (!tmp)
895
941
    {
896
942
        return;
926
972
    dbref executor,
927
973
    dbref caller,
928
974
    dbref enactor,
 
975
    int   eval,
929
976
    int key,
930
977
    char *event,
931
978
    char *cmd,
950
997
            ltd.SetSecondsString(event);
951
998
            ltaWhen += ltd;
952
999
        }
953
 
        wait_que(executor, caller, enactor, true, ltaWhen, NOTHING, 0, cmd,
954
 
            cargs, ncargs, mudstate.global_regs);
 
1000
        wait_que(executor, caller, enactor, eval, true, ltaWhen, NOTHING, 0,
 
1001
            cmd,
 
1002
            ncargs, cargs,
 
1003
            mudstate.global_regs);
955
1004
        return;
956
1005
    }
957
1006
 
1025
1074
            thing = NOTHING;
1026
1075
            bTimed = false;
1027
1076
        }
1028
 
        wait_que(executor, caller, enactor, bTimed, ltaWhen, thing, atr,
1029
 
            cmd, cargs, ncargs, mudstate.global_regs);
 
1077
        wait_que(executor, caller, enactor, eval, bTimed, ltaWhen, thing, atr,
 
1078
            cmd,
 
1079
            ncargs, cargs,
 
1080
            mudstate.global_regs);
1030
1081
    }
1031
1082
}
1032
1083
 
1039
1090
    dbref executor,
1040
1091
    dbref caller,
1041
1092
    dbref enactor,
 
1093
    int   eval,
1042
1094
    int   key,
1043
1095
    char *dbref_attr,
1044
1096
    char *dbname_query,
1276
1328
// ---------------------------------------------------------------------------
1277
1329
// do_ps: tell executor what commands they have pending in the queue
1278
1330
//
1279
 
void do_ps(dbref executor, dbref caller, dbref enactor, int key, char *target)
 
1331
void do_ps(dbref executor, dbref caller, dbref enactor, int eval, int key, char *target)
1280
1332
{
1281
1333
    UNUSED_PARAMETER(caller);
1282
1334
    UNUSED_PARAMETER(enactor);
 
1335
    UNUSED_PARAMETER(eval);
1283
1336
 
1284
1337
    char *bufp;
1285
1338
    dbref executor_targ, obj_targ;
1368
1421
    //
1369
1422
    bufp = alloc_mbuf("do_ps");
1370
1423
#ifdef QUERY_SLAVE
1371
 
    sprintf(bufp, "Totals: Wait Queue...%d/%d  Semaphores...%d/%d  SQL %d/%d",
 
1424
    mux_sprintf(bufp, MBUF_SIZE, "Totals: Wait Queue...%d/%d  Semaphores...%d/%d  SQL %d/%d",
1372
1425
        Shown_RunQueueEntry, Total_RunQueueEntry,
1373
1426
        Shown_SemaphoreTimeout, Total_SemaphoreTimeout,
1374
1427
        Shown_SQLTimeout, Total_SQLTimeout);
1375
1428
#else
1376
 
    sprintf(bufp, "Totals: Wait Queue...%d/%d  Semaphores...%d/%d",
 
1429
    mux_sprintf(bufp, MBUF_SIZE, "Totals: Wait Queue...%d/%d  Semaphores...%d/%d",
1377
1430
        Shown_RunQueueEntry, Total_RunQueueEntry,
1378
1431
        Shown_SemaphoreTimeout, Total_SemaphoreTimeout);
1379
1432
#endif // QUERY_SLAVE
1380
1433
    notify(executor, bufp);
1381
1434
    if (Wizard(executor))
1382
1435
    {
1383
 
        sprintf(bufp, "        System Tasks.....%d", Total_SystemTasks);
 
1436
        mux_sprintf(bufp, MBUF_SIZE, "        System Tasks.....%d", Total_SystemTasks);
1384
1437
        notify(executor, bufp);
1385
1438
    }
1386
1439
    free_mbuf(bufp);
1399
1452
        if (point->IsTimed)
1400
1453
        {
1401
1454
            point->waittime -= ltdWarp;
 
1455
            p->ltaWhen -= ltdWarp;
1402
1456
            return IU_UPDATE_TASK;
1403
1457
        }
1404
1458
    }
1408
1462
// ---------------------------------------------------------------------------
1409
1463
// do_queue: Queue management
1410
1464
//
1411
 
void do_queue(dbref executor, dbref caller, dbref enactor, int key, char *arg)
 
1465
void do_queue(dbref executor, dbref caller, dbref enactor, int eval, int key, char *arg)
1412
1466
{
1413
1467
    UNUSED_PARAMETER(caller);
1414
1468
    UNUSED_PARAMETER(enactor);
 
1469
    UNUSED_PARAMETER(eval);
1415
1470
 
1416
1471
    if (key == QUEUE_KICK)
1417
1472
    {
1436
1491
    else if (key == QUEUE_WARP)
1437
1492
    {
1438
1493
        int iWarp = mux_atol(arg);
1439
 
        CLinearTimeDelta ltdWarp;
1440
1494
        ltdWarp.SetSeconds(iWarp);
1441
1495
        if (scheduler.GetMinPriority() <= PRIORITY_CF_DEQUEUE_DISABLED)
1442
1496
        {